requiring a common.js module in app.js

You must Login before you can answer or comment on any questions.

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.

— answered 10 months ago by Alexander Bauer
answer permalink
6 Comments
  • Hallo Alexander, wenn ich das gleiche Modul von verschiedenen Stellen aufrufe, haben die nicht getrennte Spaces, das heißt die persistenten Daten kennen sich nicht untereinander?

    — commented 10 months ago by Rainer Schleevoigt

  • Es geht ja nicht um Daten, sondern um einen Handler der die Daten vom Backend zieht und die dann parsed. Daher gibt es im Modul selbst keine Daten. Wenn du 5 mal require() (z.B. in der app.js) machst, wird nur das 1. require tatsächlich das Modul laden. Alle nachfolgenden requires returnen das gecached module vom 1. require. Daher spielt es keine Rolle wie oft man required, solange es der selbe Context ist.

    — commented 10 months ago by Alexander Bauer

  • wenn ich aber in den Moduöen statisch Variablen speichere, sind die dann allen Instanzen bekannt? Ich ahbe eine App, die aus 4 tabs mit 4 windows besteht:

    exports.create = function() {
        var tabGroup = Titanium.UI.createTabGroup();
        var tab0 = Titanium.UI.createTab({
            icon : '/assets/navbar/systems@2x.png',
            window : require('/modules/systemswindow').create()
        });
        var win1 = require('/modules/bluewindow').create();
        var tab1 = Titanium.UI.createTab({
            icon : '/assets/navbar/glday@2x.png',
            window : win1
        });
        win1.add(require('/modules/detailview').create('getenergyday', 1, tabGroup));
        var win2 = require('/modules/bluewindow').create();
        var tab2 = Titanium.UI.createTab({
            icon : '/assets/navbar/glmonth@2x.png',
            window : win2,
        });
        win2.add(require('/modules/detailview').create('getenergymonth', 2, tabGroup));
        var win3 = require('/modules/bluewindow').create();
        var tab3 = Titanium.UI.createTab({
            icon : '/assets/navbar/glyear@2x.png',
            window : win3,
        });
        win3.add(require('/modules/detailview').create('getenergyyear', 3, tabGroup));
        win4.add(require('/modules/detailview').create('getenergytotal', 4, tabGroup));
        tabGroup.addTab(tab0);
        tabGroup.addTab(tab1);
        tabGroup.addTab(tab2);
        tabGroup.addTab(tab3);
        return tabGroup;
    }
    Wenn ich nun in der Detailview wiederum ein Modul aufrufe, ist das dann auch immer das Gleiche?

    — commented 10 months ago by Rainer Schleevoigt

  • Show 3 more comments

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.

— answered 10 months ago by Rainer Schleevoigt
answer permalink
3 Comments
  • 'every call of 'require' generate a new instance of your model'

    thats not true, using require will cache the module so its basically required once. Whether the data model is instantiated multiple times depends on the implementation of the module, as long as he wont use new keyword, nothing will be instantiated.

    — commented 10 months ago by Alexander Bauer

  • -1 for errant information as noted by Alexander

    — commented 10 months ago by Stephen Feather

  • OK,my answer was wrong. I have read in manual in every modul is other context.Thanx for hint.

    — commented 10 months ago by Rainer Schleevoigt

Your Answer

Think you can help? Login to answer this question!