Skip to content

Instantly share code, notes, and snippets.

@montlebalm
Created January 21, 2014 17:45
Show Gist options
  • Save montlebalm/8544597 to your computer and use it in GitHub Desktop.
Save montlebalm/8544597 to your computer and use it in GitHub Desktop.
Playing with sandboxing in F2 by using an AMD plugin. The plugin uses a domain key to group apps.
// Inside F2 library
// --------------------------------------------------------------------------------
// Override "define" to give us the appId inside the plugin
var oldDefine = define;
define = function(id, deps, fn) {
// Append the appId to the dependency
if (deps && deps instanceof Array) {
for (var i = 0; i < deps.length; i++) {
if (deps[i].indexOf('F2Sandbox!') === 0) {
deps[i] += '|' + id;
}
}
}
// Pass through to the normal method
oldDefine.apply(this, arguments);
};
// Make the plugin that will create new instances
define('F2Sandbox', {
load: function(name, req, onload, config) {
var parts = name.split('|');
var domain = parts[0];
var appId = parts[1];
// Make sure the appId was loaded via the specified domain
if (appIsFromDomain(appId, domain)) {
req(['F2'], function(F2) {
var domainInstance = F2.spawn(domain);
onload(domainInstance);
});
}
else {
onload.error();
}
}
});
// AppClass
// --------------------------------------------------------------------------------
define('com_domain_foo', ['F2Sandbox!domain'], function(F2) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment