5

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?

Aleksei Chepovoi
  • 3,915
  • 8
  • 39
  • 77
  • possible duplicate of [Using RequireJS, how do I pass in global objects or singletons around?](http://stackoverflow.com/questions/5608685/using-requirejs-how-do-i-pass-in-global-objects-or-singletons-around) – explunit Aug 26 '13 at 18:31
  • The first piece works as expected, as this fiddle shows, http://jsbin.com/ifeyefo/1/ . There must be something wrong with your setup. Is this really the code you use, or a simplified example. – Andreas Köberle Aug 26 '13 at 18:40
  • @Andreas Köberle, sorry, can't open the fiddle. It's approximately the code I'm using. – Aleksei Chepovoi Aug 26 '13 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 Aug 26 '13 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 Aug 26 '13 at 18:57
  • Seems you missed the import of `dataService` in your third module now – Andreas Köberle Aug 26 '13 at 19:01
  • I was typing the code manually:) In my app I didn't missed that. Updated the question. It's a very long working day.. – Aleksei Chepovoi Aug 26 '13 at 19:05
  • are you certain that the module which assigns the new value is running before the module which tries to read the value? – neonstalwart Sep 21 '13 at 03:57
  • @neonstalwart, I'm not sure, that is implied from the question) – Aleksei Chepovoi Sep 21 '13 at 05:43
  • to find out, add a console.log before each relevant line and then confirm that the sequence is what you expect. your code is being loaded asynchronously so you might need to adjust your approach to make sure your outcome is deterministic. without knowing more specifics of your actual code it's hard to advise what you might be able to do to help. – neonstalwart Sep 21 '13 at 16:44
  • This line does not seems to be right to me: `dataService.quotesArray {1, 2, 3};` . Also in the last part, the code inserted as otherModule is never executed because it is never imported. – Joqus Oct 23 '13 at 01:57

1 Answers1

7

To 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.net/kmturley/aHgMJ/

Kim T
  • 5,770
  • 1
  • 52
  • 79
  • Are the dependencies load async without any order but executed in the order - service - quotes ? – kitimenpolku Jan 30 '14 at 10:28
  • The order would be: Quotes module is defined, Service module is defined, Service module loads Quotes module and overwrites values, Sevice and Quotes modules are loaded and the values are logged out – Kim T Jan 31 '14 at 11:24