I want to share some data between different modules by creating one module called for the dataService instance, putting a variable in it, and then insert this module into the other modules as a dependency. Here is the code (which does not work):
define('dataService', function () { var quotes = []; return { quotesArray: quotes, }; }); require(['dataService'], function (dataService) { dataService.quotesArray {1, 2, 3};
Here is a 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();
I'm just wondering if some data can be made available in different modules?
And why does the first option not work?
source share