简体   繁体   English

在ribs.js中,如何捕获除纯“更改”事件以外的所有事件?

[英]In backbone.js how can I catch all events except the plain “change” event?

When I bind to the "all" event and some property changes, i receive 2 events: the "change:property" event and the plain "change" event. 当我绑定到“ all”事件并且某些属性发生更改时,我会收到2个事件:“ change:property”事件和简单的“ change”事件。 Since I always catch the property specific event , I dont need the plain one. 由于我总是赶上特定于属性的事件,因此我不需要简单的事件。 I cannot bind to all "change:*" individually because i dont know beforehand all the properties that might get added to the model. 我不能单独绑定到所有“ change:*”,因为我事先不知道可能会添加到模型中的所有属性。 So is there any way to stay with model.bind("all", ...) and somehow get rid of the "change" event? 那么,有什么方法可以与model.bind(“ all”,...)保持一致,并以某种方式摆脱“ change”事件?

PS I know I can filter the name in the handler, I am asking if there is a more standard way of filtering or declaring events in models, that I dont know of. PS我知道我可以过滤处理程序中的名称,我问是否有一种我不知道的更标准的方法来过滤或声明模型中的事件。

Unfortunately the only way is to manually add a filter 不幸的是,唯一的方法是手动添加过滤器

ModelA = Backbone.Model.extend({
  initialize: function() {
    this.bind('all', this.onChange, this);
  },

  onChange: function() {
    //apply your filter
  }
});

can't you just use the change event and then check what attributes have changed by accessing the changedAttributes method (during the propagation of change event it will return you a hash of only the attributes that have changed during this change event - see more here ). 您不能只使用change事件,然后通过访问changedAttributes方法来检查哪些属性已更改(在传播change事件的过程中,它将仅返回在此change事件期间已更改的属性的哈希值- 在此处查看更多信息 ) 。 I believe this is the cleanest way to handle that - at least based on the details provided. 我认为这是处理此问题的最干净的方法-至少基于所提供的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM