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.
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.
OR
define(['isAdmin!modelea'], function(modulea){
if (modulea) {
// doSomethingWithIt();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With