我有一個包含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