将多个属性更改绑定到 Backbone.js 模型的正确方法
我有以下代码,其中我将更改绑定到单个属性“attribute_1”。
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1", function() {
console.log('changed!');
});
}
});
如何绑定两个属性?这不起作用:
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1, change:attribute_2", function() {
console.log('changed!');
});
}
});
这也不起作用:
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1 change:attribute_2", function() {
console.log('changed!');
});
}
});
I have the following code, where I bind a change to a single attribute "attribute_1".
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1", function() {
console.log('changed!');
});
}
});
How do I bind two attributes? This doesn't work:
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1, change:attribute_2", function() {
console.log('changed!');
});
}
});
Nor does this:
var Mine = Backbone.Model.extend({
initialize: function() {
this.bind("change:attribute_1 change:attribute_2", function() {
console.log('changed!');
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Backbone.js 0.9.0 开始,
bind()
函数(已 重命名为on()
) 支持以空格分隔的事件列表:As of Backbone.js 0.9.0, the
bind()
function (which has been renamed toon()
) supports a space-delimited list of events:我不知道是否存在这样的“批量绑定”功能(您可以为其打开一个功能请求,它似乎很有用)。
您可以单独绑定它们:
或者您可以监听所有更改(然后在监听器中进行过滤):
I don't know if such a "bulk-bind" function exists (you could open a feature request for it, it seems useful).
You can bind them separately:
Or you can listen to all changes (and then filter in the listener):