Hi all, I am building a mobile app for ios/android
in the app i am using all kinds of different views that need to get XML data from a server. i am using the common.js approach so i built a module which handles all the requests to the server and formats the XML to JSON, this module will be used by all the view modules.
my question is : should i require the data module in each view module separately or can i just do this in the app.js :
var myDataModel = require('data/dataModel')will the myDataModel var pollute the global namespace or is it ok because it's a common.js module ?
Thanks
2 Answers
You will need to require it in other modules, so if you have 3 modules where you want to use it, you will need require it in each of these modules. Just keep in mind, requiring is a sort of pulling the module in the current context. Each module has its own context, therefore you need to re-require it.
As long as you are in the same context (no module. no window creation using 'url' property) you need to require it once.
Good questions! First thought: you build you module persistent and you define getters toget datas. But: every call of 'require' generate a new instance of your model. Solution: you communicate from (ui-)module with event/observer pattern. May be it is best practice.
Your Answer
Think you can help? Login to answer this question!