Created
January 21, 2014 18:23
-
-
Save montlebalm/8545332 to your computer and use it in GitHub Desktop.
This approach allows an app to specify a range of AppIds it is allowed to communicate with. Pros:
- Simple or detailed configuration
- Makes F2 plugins easy, since each app gets its own instance Cons:
- If the app is used in multiple pages, the configuration list might be very long
- It requires the app to specify every possible app it wants to …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Summary | |
In this example, each app gets its own instance of F2. Every instance of F2 | |
will share all the same page events. This setup will allow each app to add its | |
own plugins without interfering with the rest of the apps. | |
We'll add internal checks to make sure events are only broadcast to "trusted" | |
apps. This should allow any app to specify any other app on the page | |
*/ | |
// Inside F2 library | |
// -------------------------------------------------------------------------------- | |
var F2 = function(ownerId, trustedAppIds) { | |
this._trustedIds = trustedAppIds || ['*']; | |
}; | |
// Internal map of created instances | |
var _instances = { | |
'com_mod_foo': null | |
}; | |
// Override AMD's "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 a plugin that will create new instances | |
define('F2Sandbox', { | |
load: function(name, req, onload, config) { | |
var allowedIds = name.split('|'); | |
var appId = allowedIds.pop(); | |
if (!_instances[appId]) { | |
_instances[appId] = new F2(appId, allowedIds); | |
} | |
// Pass back the new reference | |
onload(_instances[appId]); | |
} | |
}); | |
// AppClass | |
// -------------------------------------------------------------------------------- | |
define('com_mod_foo', ['F2Sandbox!com_mod_*|com_client_*'], function(F2) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment