简体   繁体   English

将数据传递到变更事件回调中?

[英]Pass data into change event callback?

Using backbone.js - I want to bind an event to any change in the model and the collections/models that are nested inside it. 使用骨干.js-我想将事件绑定到模型以及嵌套在其中的集合/模型的任何更改。

Right now I do an @bind 'change', () -> when initializing the base model. 现在@bind 'change', () ->在初始化基本模型时@bind 'change', () ->我会执行@bind 'change', () ->

How can I pass data on any change, even that of deep nested models? 我如何传递任何变化的数据,甚至包括深层嵌套模型的变化? Does the change event carry variables with it? 变更事件是否附带变量? I need things like the model's collection, id, attributes, etc. 我需要诸如模型的集合,ID,属性等之类的东西。

Thanks. 谢谢。

Does the change event carry variables with it? 变更事件是否附带变量?

Yes, the change event gets two arguments: First, the model itself; 是的, change事件有两个参数:第一,模型本身;第二,模型本身。 second, the new attribute value. 第二,新的属性值。 There are several methods available on the model that are aimed specifically at getting information during a change event. 模型上有几种可用的方法,这些方法专门用于 change事件期间获取信息。 See the docs on hasChanged , changedAttributes , previous , and previousAttributes . 查看该文档hasChangedchangedAttributes以前previousAttributes

So for instance, to access the previous attributes of a model each time it changes, you'd write 因此,例如,要在模型每次更改时访问模型的先前属性,您可以编写

@bind 'change', (model) ->
  prevAttrs = model.previousAttributes()
  ...

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

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