View doesn't listen to Model Event Backbone.js
View doesn't listen to Model Event Backbone.js
由于某些我无法理解的原因,我的View没有监听事件。这种模式正在改变,但人们的观点似乎没有意识到这些变化。这是我的代码。
var playerSet = 1;
var bone = function(){
var app = {};
app.BoardModel = Backbone.Model.extend({
defaults: function(){
return{
board:[0,0,0,0,0,0,0,0,0],
allDisabled: false,
p1Score: 0,
p2Score: 0
}
},
setSlot: function(slot, ct){
var b = this.get("board");
b[slot] = ct;
this.set("board", b);
console.log("CHANGED");
}
});
app.Board = new app.BoardModel;
app.BoardView = Backbone.View.extend({
el: $("#ttt-board"),
initialize: function(){
this.listenTo(app.Board, "change", this.renderBoard);
},
renderBoard: function(){
console.log("HELLO THERE");
}
});
var tictac = new app.BoardView;
app.Board.setSlot(0,1);
};
bone();
当我触发setSlot
时,模型确实随着控制台输出CHANGED
而改变,但是我从未看到renderBoard函数被调用。
您的问题是您只更改数组对象的内部组件,而不是模型上的属性。即使您在模型上手动调用set
,这也不是实际的更改,set
逻辑仅在旧值和新值之间的相等性检查失败时触发更改事件(在您的情况下它没有)。
既然你正在调用自定义函数,为什么不使用自定义事件呢?
setSlot: function(slot, ct){
this.get("board")[slot] = ct;
this.trigger("custom:change:board", slot, ct);
}
现在监听自定义事件(或两者),而不仅仅是change
。
initialize: function(){
this.listenTo(app.Board, "change custom:change:board", this.renderBoard);
}
相关文章:
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- Ember Data DS.Model's set函数不起作用
- jQuery AJAX write to XML
- grep in JQuery to C#
- how to convert html <div> to pdf
- Node.js - POST to iFrame?
- SVG xml to image
- C#asp.net mvc Set CheckBoxFor已检查的具有Model Value的属性
- Advantages to DOMParser vs template & innerHTML
- how to split a string with ','
- How to declare a Map containing certain properties with flow
- Rails将JavaScript对象存储到Model的有效方式
- Php variable obj to js
- 如何在submit to sightly时将表单输入值作为参数传递给WCMUsePojo
- Ember model to json
- Backbone.js fetch() JSON to model get() returns undefined
- View doesn't listen to Model Event Backbone.js
- Sails.js: save() model doesn't seems to work Error (E_UN
- AngularJS: prepend "http://" to ng-model
- Ember js controllerSetup with call to async model使用断点工作,但不能没