*更新:已解决,请向下滚动*
使用下面的内容,列表会呈现,但不会呈现任何行。这是因为我没有在复合视图中定义集合。
如果我确实 put:collection: new Users in list.js 然后我得到这个错误:
Uncaught TypeError: Object function () {
return parent.apply(this, arguments);
} has no method 'on'
在backbone.js:第203行,即:
// An inversion-of-control version of `on`. Tell *this* object to listen to
// an event in another object ... keeping track of what it's listening to.
listenTo: function (obj, name, callback) {
var listeners = this._listeners || (this._listeners = {});
var id = obj._listenerId || (obj._listenerId = _.uniqueId('l'));
listeners[id] = obj;
obj.on(name, typeof name === 'object' ? this : callback, this);
return this;
},
意见
行.js
define([
'marionette',
'text!app/views/templates/user/row.html'
],
function (Marionette, Template) {
"use strict"
return Marionette.ItemView.extend({
template: Template,
tagName: 'tr'
})
})
列表.js
define([
'marionette',
'text!app/views/templates/user/list.html',
'app/collections/users',
'app/views/user/row'
],
function (Marionette, Template, Users, User) {
"use strict"
return Backbone.Marionette.CompositeView.extend({
template: Template,
itemView: User,
itemViewContainer: "tbody",
})
})
通过这样做解决了这个问题:
用户/list.js
define([
'marionette',
'text!app/views/templates/user/list.html',
'app/collections/users',
'app/views/user/row'
],
function (Marionette, Template, Users, User) {
"use strict"
return Backbone.Marionette.CompositeView.extend({
template: Template,
itemView: User,
itemViewContainer: "tbody",
initialize: function() {
this.collection = new Users()
this.collection.fetch()
}
})
})
用户/row.js
define([
'marionette',
'text!app/views/templates/user/row.html'
],
function (Marionette, Template) {
"use strict"
return Backbone.Marionette.ItemView.extend({
template: Template,
tagName: "tr"
})
})