;(function(){
var mythings = {};
function initializer($, _, Backbone, tableModel){
return Backbone.Collection.extend({
url: 'main-contact', <!-- this used to be this.url.
model: tableModel,
initialize: function(models, options) {
this.fetch();
}
});
}
define([
'jquery',
'underscore',
'backbone',
'models/tableModel'
],
function($, _, Backbone, tableModel) {
if (!mythings.tablesCollection){
// this will be done on first run.
mythings.tablesCollection = initializer($, _, Backbone, tableModel);
}
// all others will just return same exact instance of collection class
return mythings.tablesCollection;
});
})();
首先,块开头的分号是什么意思?其次,我需要在运行时在我的路由中传入一个URL,如下所示:
var t = new tablesCollection(null, { url: 'main-contact'} );
这是我一直在做的事情,当时我正在尝试将此项目重新创建为AMD。如何在运行时将URL传递到自调用代码块中?
编辑:
define([
'jquery',
'underscore',
'backbone',
'models/tableModel',
'collections/tablesCollection',
'views/tablesView'
], function($, _, Backbone, tableModel, tablesCollection, tablesView) {
require(['collections/tablesCollection'], function(tablesCollection) {
var t = new tablesCollection(null, {url: 'main-contact'});
var tables = new tablesView({ collection: t, template: 'main-contact-template'});
$('#web-leads').html(tables.render().el);
});
});
答案 0 :(得分:0)
首先,正如其他评论者所说,你不需要额外的“模块模式”包装;您的代码可以简化为:
define(['jquery',
'underscore',
'backbone',
'models/tableModel'
], function($, _, Backbone, tableModel) {
return Backbone.Collection.extend({
url: 'main-contact', //<-- this used to be this.url.
model: tableModel,
initialize: function(models, options) {
this.fetch();
}
});
});
然后,在其他地方,您可以这样做:
require(['path/to/TablesCollection', function(TablesCollection) {
var t = new TablesCollection(null, {url: 'main-contact'});
});
传递URL,就像你想要的那样......差不多。看到最后一个问题,即您的收藏集不知道使用选项中的URL;让我们解决一下:
initialize: function(models, options) {
this.url = options.url;
this.fetch();
}
有了这个你应该全部设定(但如果有任何混淆,请回复)。