将多个属性更改绑定到 Backbone.js 模型的正确方法

发布于 2024-12-18 02:26:16 字数 672 浏览 0 评论 0原文

我有以下代码,其中我将更改绑定到单个属性“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-12-25 02:26:16

从 Backbone.js 0.9.0 开始,bind() 函数(已 重命名为 on()) 支持以空格分隔的事件列表:

model.on("change:title change:author", ...)

// equivalent to

model.bind("change:title change:author", ...)

As of Backbone.js 0.9.0, the bind() function (which has been renamed to on()) supports a space-delimited list of events:

model.on("change:title change:author", ...)

// equivalent to

model.bind("change:title change:author", ...)
↙厌世 2024-12-25 02:26:16

我不知道是否存在这样的“批量绑定”功能(您可以为其打开一个功能请求,它似乎很有用)。

您可以单独绑定它们:

var Mine = Backbone.Model.extend({
  initialize: function() {
    var listener = function() { console.log('changed'); };
    this.bind("change:attribute_1", listener);
    this.bind("change:attribute_2", listener);
  }
});

或者您可以监听所有更改(然后在监听器中进行过滤):

var Mine = Backbone.Model.extend({
  initialize: function() {
    var listener = function() { console.log('changed'); };
    this.bind("change", listener);
  }
});

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:

var Mine = Backbone.Model.extend({
  initialize: function() {
    var listener = function() { console.log('changed'); };
    this.bind("change:attribute_1", listener);
    this.bind("change:attribute_2", listener);
  }
});

Or you can listen to all changes (and then filter in the listener):

var Mine = Backbone.Model.extend({
  initialize: function() {
    var listener = function() { console.log('changed'); };
    this.bind("change", listener);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文