backbone.js 模型不触发事件
我有一个视图似乎不想渲染,因为模型的更改事件没有触发。
这是我的模型:
var LanguagePanelModel = Backbone.Model.extend({
name: "langpanel",
url: "/setlocale",
initialize: function(){
console.log("langselect initing")
}
})
这是我的视图:
var LanguagePanelView = Backbone.View.extend({
tagName: "div",
className: "langselect",
render: function(){
this.el.innerHTML = this.model.get("content");
console.log("render",this.model.get(0))
return this;
},
initialize : function(options) {
console.log("initializing",this.model)
_.bindAll(this, "render");
this.model.bind('change', this.render);
this.model.fetch(this.model.url);
}
});
这是我实例化它们的方式:
if(some stuff here)
{
lsm = new LanguagePanelModel();
lsv = new LanguagePanelView({model:lsm});
}
我获取了初始化的日志,但没有获取视图渲染的日志? 有什么想法吗?
I have a view which doesn't seem to want to render as the model's change event is not firing.
here's my model:
var LanguagePanelModel = Backbone.Model.extend({
name: "langpanel",
url: "/setlocale",
initialize: function(){
console.log("langselect initing")
}
})
here's my view:
var LanguagePanelView = Backbone.View.extend({
tagName: "div",
className: "langselect",
render: function(){
this.el.innerHTML = this.model.get("content");
console.log("render",this.model.get(0))
return this;
},
initialize : function(options) {
console.log("initializing",this.model)
_.bindAll(this, "render");
this.model.bind('change', this.render);
this.model.fetch(this.model.url);
}
});
here's how I instantiate them:
if(some stuff here)
{
lsm = new LanguagePanelModel();
lsv = new LanguagePanelView({model:lsm});
}
I get logs for the init but not for the render of the view?
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我添加的问题已解决
The issue was resolved my adding
与博士建议的不同,您不需要预定义属性。您需要将上下文传递给“bind” -
this.model.bind('change', this.render, this);
请参阅 http://jsfiddle.net/7LzTt/ 或以下代码:
You don't need attributes to be predefined unlike PhD suggested. You need to pass the context to 'bind' -
this.model.bind('change', this.render, this);
See working fiddle at http://jsfiddle.net/7LzTt/ or code below:
我想这是关于设置模型的属性 - name 不是标准属性,并且您定义它的方式,它似乎可以通过使用
model.name
直接访问,而主干不允许那个据我所知。以下是有效的更改:) 您也可以看到相关的 fiddle :)更新:
您不需要手动触发更改事件 - 将其视为不好的做法。这是主干文档的内容(注意:获取也会触发更改!)
获取
因此,如果从服务器获取的值与默认值不同,则会触发更改事件,因此您无需自己执行此操作。如果您确实希望有这样的事件,那么您可以使用触发器方法,但自定义名称,因为它特定于您的应用程序。可以这么说,您基本上是在尝试使事件超载。完全没问题,但只是一个想法。
改变
仅当您通过将
silent:true
作为参数传递给模型的set
方法来抑制事件时,才需要手动触发更改事件。您可能还想查看主干文档中的“已更改”和其他事件。
编辑忘记为上面的示例添加更新的小提琴 - 你可以看到当通过显式调用
set
更改模型时,警报框会弹出两次 - 获取时也会发生同样的情况。因此,关于您“可能不需要”需要像您所做的那样手动触发“更改”这一事实的评论:)I guess it's about setting the attributes of the model - name is not a standard attribute and the way you've defined it, it seems to be accessible directly by using
model.name
and backbone doesn't allow that AFAIK. Here are the changes that work :) You can see the associated fiddle with it too :)UPDATE:
You don't need to manually trigger the change event - think of it as bad practice. Here's what the backbone documentation says (note: fetch also triggers change!)
Fetch
So, if the value fetched from the server is different from the defaults the change event will be fired so you needn't do it yourself. If you really wish to have such an event then you can use the trigger approach but custom name it since it's specific to your application. You are basically trying to overload the event so to speak. Totally fine, but just a thought.
Change
The change event is to be manually triggered only if you've been suppressing the event by passing
silent:true
as an argument to theset
method of the model.You may also want to look at 'has changed' and other events from the backbone doc.
EDIT Forgot to add the updated fiddle for the above example - you can see that the alert box pops up twice when the model is changed by explicitly calling
set
- the same would happen on fetching too. And hence the comment on the fact that you "may not" need to trigger 'change' manually as you are doing :)