Using backbone.js – I want to bind an event to any change in the model and the collections/models that are nested inside it.
Right now I do an @bind 'change', () -> when initializing the base model.
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.
Thanks.
Yes, the
changeevent gets two arguments: First, the model itself; second, the new attribute value. There are several methods available on the model that are aimed specifically at getting information during achangeevent. See the docs on hasChanged, changedAttributes, previous, and previousAttributes.So for instance, to access the previous attributes of a model each time it changes, you’d write