Search code examples
javascriptbackbone.jsrequirejslazy-loadingjs-amd

how backbone requirejs works for my case?


I am building an application using requirejs and backbone, I would like to load modules asynchronously after some actions.

say I have some modules like

authentication
module1
module2
module3
module4
platform
utils

Initially I need to load authentication module and after success

I would like to load particular module(views, models,collections) based on route using requirejs.

how can I load modules as explained?

Please suggest, if there is any boilerplate available for this pattern?


Solution

  • If you using marionette module:

    In module you can use

    startWithParent = false
    

    And then start the module in the desired route

    For example you have routes defined as

    appRoutes: {
            'login': 'login'
    }
    

    In the controller

    login: function(){
           // you can require the login module here
          // if you using backbone only you can require views, models, collections in the       
          //similar way in the desired routes
          require(['modules/LoginModule'], function(LoginModule) {
                App.module('LoginModule').start();
          });
    }
    

    When you want to stop the module

     App.module('LoginModule').stop();