Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RequireJS modules sharing data
//requireJS shared instances
require.config({
paths: {
}
});
define("one", function() {
return { sharedVariable:"one" };
});
define("two",["one"], function(one) {
return {
setShared:function(value){
one.sharedVariable=value;
},
getShared:function(){
return one.sharedVariable;
}};
});
define("three", ["one"], function(one) {
return {
setShared:function(value){
one.sharedVariable=value;
},
getShared:function(){
return one.sharedVariable;
}};
});
require(["two","three"], function(two,three) {
console.log("two:"+two.getShared());
console.log("three:"+three.getShared());
two.setShared("TWO");
console.log("two:"+two.getShared());
console.log("three:"+three.getShared());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment