Skip to content

Instantly share code, notes, and snippets.

@hadaytullah
Last active October 25, 2019 09:57
Show Gist options
  • Save hadaytullah/be189f7a69e1eee351b7d817c3ed0d48 to your computer and use it in GitHub Desktop.
Save hadaytullah/be189f7a69e1eee351b7d817c3ed0d48 to your computer and use it in GitHub Desktop.
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