EasyXDM allows you to have multiple instances of it per site. You can do this using noConflict
.
For example, if you're building JavaScript that will go on a site you do not control, you can always create an instance of EasyXDM and set it to whatever you want.
I do this for our JavaScript widgets (the usage can be viewed here). Each script can then call ns.NSEasyXDM
and have a reference to it without pummeling the easyXDM in the global namespace (since it likes to put itself on the window
).
If you need to talk to multiple endpoints, you can send in a different consumerRpcConfig
and consumerJsonRpcConfig
as needed.
this.ns = this.ns || {};
(function(ns, window, document) {
var base = this,
consumerRpcConfig = {
remote: document.location.protocol+ "//my.path.org/Scripts/easyXDM/cors/"
},
consumerJsonRpcConfig = {
remote: {
request: {}
}
},
init = function(el, callback) {
var easyXDMElement,
scripts = document.getElementsByTagName('script'),
scriptIdx;
for (scriptIdx = 0; scriptIdx < scripts.length; scriptIdx = scriptIdx + 1) {
if (scripts[scriptIdx].src === document.location.protocol + '//my.path.org/Scripts/EasyXDM/easyXDM.js') {
base.isEasyXDMPresent = true;
}
}
if (!base.isEasyXDMPresent || ns.NSEasyXDM === undefined) {
easyXDMElement = document.createElement('script');
easyXDMElement.type = 'text/javascript';
easyXDMElement.src = document.location.protocol + '//my.path.org/Scripts/EasyXDM/easyXDM.js';
el.parentNode.insertBefore(easyXDMElement, el);
}
easyXDMElement.onload = function () {
ns.NSEasyXDM = { easyXDM: window.easyXDM.noConflict("NSEasyXDM") };
callback();
};
}
return ns;
}(this.ns, window, document));
To define multiple copies of EasyXDM as shown above, use the "noConflict", assign each instance to a variable; and then you can pass that around.