我有一個名爲layerPanel的視圖,該視圖使用screenData模型。現在在model.set上,我從模型本身獲取更新事件,但它不能處理視圖。在骨幹js中更新模型時,更改事件不會觸發
模型
var screenData = Backbone.Model.extend({
initialize : function() {
_.bindAll(this,"update");
this.bind('change:vdata', this.update);
},
update: function() {
var obj = this.vdata;
alert("update");
},
vdata:[{id : 0, title : "Welcome to Persieve 0"}]
});
VIEW
var layerPanel = Backbone.View.extend({
el: "#allLayers",
model: new screenData(),
initialize: function() {
this.render();
this.model.bind('change:vdata', this.render);
},
render: function() {
this.template = _.template(LayersTpl, {splitData: this.model.vdata});
this.$el.html(this.template);
return this;
}
});
下面是我在模型中設置的值。
screendata = new screenData;
var obj = screendata.vdata;
obj[obj.length] = {id : $(".bullet").length, title : "Welcome to Persieve"};
var tempData = [];
for (var index=0; index<obj.length; index++) {
if (obj[index]) {
tempData.push(obj);
}
}
obj = tempData;
screendata.set({vdata:[obj]});
你能展示更多你的價值設置代碼嗎?您是否將layerPanel的模型設置爲您在最後一個代碼段的第一行中創建的新模型? – 2013-03-27 12:03:56
Matt,我已經添加了在Model中設置值的代碼。 – 2013-03-27 14:13:06