Requirejs - Loading modules only if the condition is true

Using Requirejs, how can I load modules only if the condition is true? For example, if the user is an administrator, upload the file to the a.js. module

PS: I use Backbone with Requirejs.

+5
source share
2 answers

Something like that?

define([], function () {
    function realWork (modulea) {
        // do stuff ...
        // so stuff with modulea
        if (modulea) {
            ...
        }
    }

    if (isAdmin) {
        require(["modulea"], function (modulea) {
            realWork(modulea);
        });
    } else {
        realWork();
    }
});

You may be able to write your own requirejs plugin to remove this if you find yourself repeating the pattern.

+5
source

OR

define(['isAdmin!modelea'], function(modulea){ 
  if (modulea) { 
    // doSomethingWithIt(); 
  } 
}); 
+1
source

All Articles