我有一个包含CellCollection CellModels的BoardView。我从数据库获取集合,然后创建CellViews。backbone.js - 从点击事件访问模型
这一切都在顺利进行,直到我尝试通过BoardView上的单击事件访问CellModel为止。我根本无法得到底层模型......只有意见。有没有办法做到这一点?
我已经尝试包括以下相关代码:
CellModel = Backbone.Model.extend({});
CellCollection = Backbone.Collection.extend({
model : CellModel
});
CellView = Backbone.View.extend({
className : 'cell',
});
BoardView = Backbone.View.extend({
this.model.cells = new CellCollection();
render : function() {
this.cellList = this.$('.cells');
return this;
},
allCells : function(cells) {
this.cellList.html('');
this.model.cells.each(this.addCell);
return this;
},
addCell : function(cell) {
var view = new Views.CellView({
model : cell
}).render();
this.cellList.append(view.el);
},
events : {
'click .cell' : 'analyzeCellClick',
},
analyzeCellClick : function(e) {
// ?????????
}
});
我需要点击BoardView,而不是CellView为“发生”,因为它涉及特定板的逻辑。
OP在这里。两个解决方案都完美无缺地工作......并且#1对于我的目的来说是完美的。非常感谢。 –
太好了 - 如果您对答案感到满意,您可以点击分数下方的复选框来接受它:)。 – nrabinowitz
解决方案2在我看来并不是一个好主意。 Backbone的重点是从服务器获取数据并将其呈现在客户端视图中,而不是从视图中获取数据并发回。它有效,但它不是一个好方法。 – sntran