I'm using the Backbone/RequireJs javascript stack. What i want to achieve is to dynamically require the Model dependencies for a Backbone.Collection, after that Collection dependency is set.
I want to keep that Collection as much generic as possible and i want to assign different Models and rest urls depending on the main current context. The goal is, well, loading only the needed in a particular context.
window.Collection = Backbone.Collection.extend({
initialize: function(modelName,apiRoute){
this.modelName = modelName;
this.apiRoute = apiRoute;
if (typeof(window[this.modelName]) === "function") {
this.model = window[this.modelName];
this.url = "./api/" + this.apiRoute;
}
}
...
The problem here is that window[this.modelName] dependency in the main context must be loaded before the Collection dependency.
Say the object MainContext needs a Collection:
// MainContext.js
define([
...
"model/Collection"
and inside MainContext i resolve the modelName variable needed for the name value of the required dependency for the Collection (e. 'Toys'). If i require(['model/Toys']) inside MainContext, that dependency file will be loaded too late.
What would be the approach in a case like that? Appreciate.