Hi i am trying to load some requireJs modules in a dynamic way by getting a list of filters and the iterate over the array to load this modules like this
define(function(require){
var runFilters = function(filters){
var _ = require('underscore');
var computedFilters = getFilters(filters);
var result = _.every(computedFilters,function(filter){
return filter();
});
return result;
};
var getFilters = function(filters){
var _ = require('underscore');
return _.map(filters,function(filter){
return require('filters/' + filter);
},this);
}
var register = function(filters,fn){
return function(){
var args = Array.prototype.slice.apply(arguments);
if(runFilters(filters))
fn.apply(this,args);
}
}
return{
register: register
}
});
This give me error: Uncaught Error: Module name "filters/isAuth" has not been loaded yet for context: _
but when replacing this to a static way (just to test) it loads perfectly
define(function(require){
var runFilters = function(computedFilters){
var result = _.every(computedFilters,function(filter){
return filter();
});
return result;
};
var getFilters = function(filters){
var _ = require('underscore');
return _.map(filters,function(filter){
console.log(filter);
return require('filters/' + filter);
},this);
}
var register = function(filters,fn){
var cachedFilters = [];
cachedFilters.push(require('filters/isAuth'));
return function(){
var args = Array.prototype.slice.apply(arguments);
if(runFilters(cachedFilters))
fn.apply(this,args);
}
}
return{
register: register
}
});
this give me the error too
cachedFilters.push(require('filters'+'/isAdmin'));
See Question&Answers more detail:os