【发布时间】:2013-03-27 11:39:37
【问题描述】:
我有一个使用 screenData 模型的名为 layerPanel 的视图。现在在 model.set 上,我从模型本身获取更新事件,但它在视图上不起作用。
型号
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"}]
});
查看
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的model设置成上次sn-p第一行新建的model?
-
Matt,我添加了用于在模型中设置值的代码。
标签: backbone.js backbone-events