Search code examples
backbone.js

hasChanged useless if "only useful during the course of a 'change' event"?


Seems confusing to have a hasChanged() method you can call on a model, but which is only "useful" during a change event. Doesn't the fact that a change event has been fired mean the model has changed?

Is the only usage of hasChanged() to detect if a model has changed if user-written code is manually triggering the model's change event?


Solution

  • Is the only usage of hasChanged() to detect if a model has changed if user-written code is manually triggering the model's change event?

    This is certainly one strong use-case for the .hasChanged method, but another - potentially far more powerful - is checking for a single property on a generic change event.

    From the documentation:

    If an attribute is passed, returns true if that specific attribute has changed.

    This means a single change binding can decide what to do based on whether individual properties have changed. It's likely this is the more useful of the two method signatures.