Share resources across different amd modules

后端 未结 5 980
不思量自难忘°
不思量自难忘° 2020-12-08 03:38

I\'m currently developing a new web application.

This is the first time I\'m using requirejs with AMD modules.

It\'s not that easy to get used to tha

5条回答
  •  一个人的身影
    2020-12-08 03:57

    To answer your question as to how to provide an application wide event object, you can create an amd module called globalContext and instantiate it in the main.js.
    Thereafter you can attach settings to the globalContext and use the global context to create subcomponents etc.

        //main.js file
        require([ "./global-context" ], function( GlobalContext) {
                 new GlobalContext();
        });
    

    In the global-context.js file we can then perform tasks such as loading child modules

        define(["Boiler", "./settings", "./modules/modules"], 
               function (Boiler, settings, modules) {
                      var GlobalContext = function () {
    
                    //here we use a template javascript class called Boiler.Context
                    var globalContext = new Boiler.Context("GlobalModule");
    
                    //add settings to the context which can be obtained globally throughout                    the application
                    globalContext.addSettings(settings);
    
                    //here we load the sub modules of the global context
                    globalContext.loadChildModules(modules);
    };
    

    This is what we have implemented in BoilerplateJS, a reference architecture for large scale javascript product development.

提交回复
热议问题