admin管理员组文章数量:1130349
I want to share some data between different modules by creating one module, called for instance dataService, put a variable into it, and then insert this module in other modules as a dependency. Here is the code (that doesn't work):
define('dataService', function () {
var quotes = [];
return {
quotesArray: quotes,
};
});
require(['dataService'], function (dataService) {
dataService.quotesArray {1, 2, 3}; // setting the quotes variable
});
define('otherModule', ['dataService'], function (dataService) {
var x = dataService.quotesArray; // x = empty Array, why?
});
Here is the workaround:
define('dataService', function () {
var quotes = [];
var getQuotes = function () {
return quotes;
};
var setQuotes = function (newQuotes) {
quotes = newQuotes;
};
return {
getQuotes: getQuotes,
};
});
require(['dataService'], function (dataService) {
var x = dataService.getQuotes(); // now I can get/set the quotes variable
dataService.setQuotes();
});
I'm just wondering if it is a proper way to make some data be accessible in different modules?
And why first option doesn't work?
I want to share some data between different modules by creating one module, called for instance dataService, put a variable into it, and then insert this module in other modules as a dependency. Here is the code (that doesn't work):
define('dataService', function () {
var quotes = [];
return {
quotesArray: quotes,
};
});
require(['dataService'], function (dataService) {
dataService.quotesArray {1, 2, 3}; // setting the quotes variable
});
define('otherModule', ['dataService'], function (dataService) {
var x = dataService.quotesArray; // x = empty Array, why?
});
Here is the workaround:
define('dataService', function () {
var quotes = [];
var getQuotes = function () {
return quotes;
};
var setQuotes = function (newQuotes) {
quotes = newQuotes;
};
return {
getQuotes: getQuotes,
};
});
require(['dataService'], function (dataService) {
var x = dataService.getQuotes(); // now I can get/set the quotes variable
dataService.setQuotes();
});
I'm just wondering if it is a proper way to make some data be accessible in different modules?
And why first option doesn't work?
Share Improve this question edited Aug 26, 2013 at 19:04 Aleksei Chepovoi asked Aug 26, 2013 at 18:09 Aleksei ChepovoiAleksei Chepovoi 3,9559 gold badges42 silver badges77 bronze badges 11- possible duplicate of Using RequireJS, how do I pass in global objects or singletons around? – Steve P Commented Aug 26, 2013 at 18:31
- The first piece works as expected, as this fiddle shows, jsbin./ifeyefo/1 . There must be something wrong with your setup. Is this really the code you use, or a simplified example. – Andreas Köberle Commented Aug 26, 2013 at 18:40
- @Andreas Köberle, sorry, can't open the fiddle. It's approximately the code I'm using. – Aleksei Chepovoi Commented Aug 26, 2013 at 18:44
-
As I say, your example code works fine. Most of the time a required module is
undefined, there is a circular reference between the modules. So does the first module requires also the second module in your real code. – Andreas Köberle Commented Aug 26, 2013 at 18:49 - @Andreas Köberle, sorry for missleading you, I made a mistake. Please, reread the question. I can query dataService.quotesArray, but if I set it in one module, and then query dataService.quotesArray in another module, I get empty array, seems like I haven't set quotes var in a first module – Aleksei Chepovoi Commented Aug 26, 2013 at 18:57
1 Answer
Reset to default 7To make this work you need to create an instance of both, so one overwrites the properties of the other:
define('Quotes', function (Module) {
return {
quotesArray: ['a', 'b', 'c']
};
});
define('Service', ['Quotes'], function (quotes) {
console.log(1, quotes.quotesArray); // ["a", "b", "c"]
quotes.quotesArray = [1, 2, 3];
});
require(['Service', 'Quotes'], function(service, quotes) {
console.log(2, quotes.quotesArray); // [1, 2, 3]
});
Here's a working fiddle: http://jsfiddle/kmturley/aHgMJ/
I want to share some data between different modules by creating one module, called for instance dataService, put a variable into it, and then insert this module in other modules as a dependency. Here is the code (that doesn't work):
define('dataService', function () {
var quotes = [];
return {
quotesArray: quotes,
};
});
require(['dataService'], function (dataService) {
dataService.quotesArray {1, 2, 3}; // setting the quotes variable
});
define('otherModule', ['dataService'], function (dataService) {
var x = dataService.quotesArray; // x = empty Array, why?
});
Here is the workaround:
define('dataService', function () {
var quotes = [];
var getQuotes = function () {
return quotes;
};
var setQuotes = function (newQuotes) {
quotes = newQuotes;
};
return {
getQuotes: getQuotes,
};
});
require(['dataService'], function (dataService) {
var x = dataService.getQuotes(); // now I can get/set the quotes variable
dataService.setQuotes();
});
I'm just wondering if it is a proper way to make some data be accessible in different modules?
And why first option doesn't work?
I want to share some data between different modules by creating one module, called for instance dataService, put a variable into it, and then insert this module in other modules as a dependency. Here is the code (that doesn't work):
define('dataService', function () {
var quotes = [];
return {
quotesArray: quotes,
};
});
require(['dataService'], function (dataService) {
dataService.quotesArray {1, 2, 3}; // setting the quotes variable
});
define('otherModule', ['dataService'], function (dataService) {
var x = dataService.quotesArray; // x = empty Array, why?
});
Here is the workaround:
define('dataService', function () {
var quotes = [];
var getQuotes = function () {
return quotes;
};
var setQuotes = function (newQuotes) {
quotes = newQuotes;
};
return {
getQuotes: getQuotes,
};
});
require(['dataService'], function (dataService) {
var x = dataService.getQuotes(); // now I can get/set the quotes variable
dataService.setQuotes();
});
I'm just wondering if it is a proper way to make some data be accessible in different modules?
And why first option doesn't work?
Share Improve this question edited Aug 26, 2013 at 19:04 Aleksei Chepovoi asked Aug 26, 2013 at 18:09 Aleksei ChepovoiAleksei Chepovoi 3,9559 gold badges42 silver badges77 bronze badges 11- possible duplicate of Using RequireJS, how do I pass in global objects or singletons around? – Steve P Commented Aug 26, 2013 at 18:31
- The first piece works as expected, as this fiddle shows, jsbin./ifeyefo/1 . There must be something wrong with your setup. Is this really the code you use, or a simplified example. – Andreas Köberle Commented Aug 26, 2013 at 18:40
- @Andreas Köberle, sorry, can't open the fiddle. It's approximately the code I'm using. – Aleksei Chepovoi Commented Aug 26, 2013 at 18:44
-
As I say, your example code works fine. Most of the time a required module is
undefined, there is a circular reference between the modules. So does the first module requires also the second module in your real code. – Andreas Köberle Commented Aug 26, 2013 at 18:49 - @Andreas Köberle, sorry for missleading you, I made a mistake. Please, reread the question. I can query dataService.quotesArray, but if I set it in one module, and then query dataService.quotesArray in another module, I get empty array, seems like I haven't set quotes var in a first module – Aleksei Chepovoi Commented Aug 26, 2013 at 18:57
1 Answer
Reset to default 7To make this work you need to create an instance of both, so one overwrites the properties of the other:
define('Quotes', function (Module) {
return {
quotesArray: ['a', 'b', 'c']
};
});
define('Service', ['Quotes'], function (quotes) {
console.log(1, quotes.quotesArray); // ["a", "b", "c"]
quotes.quotesArray = [1, 2, 3];
});
require(['Service', 'Quotes'], function(service, quotes) {
console.log(2, quotes.quotesArray); // [1, 2, 3]
});
Here's a working fiddle: http://jsfiddle/kmturley/aHgMJ/
本文标签: How to share data between different modules in requirejsJavaScriptStack Overflow
版权声明:本文标题:How to share data between different modules in requirejs, javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1745127190a2135927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论