Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requirejs - Loading modules only if a condition is true

Using Requirejs, how can I load modules only if a condition is true? example, If the user is Admin, load file the module a.js.

PS: I'm using Backbone with Requirejs.

like image 509
keepyourweb Avatar asked Apr 26 '12 10:04

keepyourweb


2 Answers

Something like this?

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

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

You might be able to write your own requirejs plugin to tidy this up if you find yourself repeating the pattern.

like image 132
Paul Grime Avatar answered Oct 19 '22 02:10

Paul Grime


OR

define(['isAdmin!modelea'], function(modulea){ 
  if (modulea) { 
    // doSomethingWithIt(); 
  } 
}); 
like image 31
Bogdan M. Avatar answered Oct 19 '22 02:10

Bogdan M.