Return to Snippet

Revision: 52438
at October 23, 2011 13:54 by ultranaut


Initial Code
(function(global) {
	var Sandbox = function(modules, callback) {
		if (!(this instanceof Sandbox)) {
			return new Sandbox(modules, callback);
		}
		// modules is an array in this instance:
		for (var i = 0, len = modules.length; i < len; i++) {
			installedModules[modules[i]](this);
		}
		callback(this);
	};
	Sandbox.modules = {};
	global.Sandbox = Sandbox;
})(this);

// Example module:
// You extend the current sandbox instance with new functions
Sandbox.modules.ajax = function(sandbox) {
	sandbox.ajax = $.ajax;
	sandbox.json = $.getJSON;
};

// Example of running your code in the sandbox on some page:
Sandbox(['ajax'], function(sandbox) {
	sandbox.ajax({
		type: 'post',
		url: '/Sample/Url',
		success: function(response) {
			// success code here. remember this ajax maps back to $.ajax
		}
	});
});

Initial URL

                                

Initial Description
http://stackoverflow.com/questions/3628649/javascript-self-contained-sandbox-events-and-client-side-stack

Initial Title
JS sandbox pattern implementation

Initial Tags
javascript

Initial Language
JavaScript