我从上一篇文章中得到了这段代码可以使用:
;(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);
});
});