jashkenas/backbone — pull request #3386 by Prinzhorn (2014-11-18 12:51:21+00:00)
Title: Added `sparse` option to `set`

This option is useful for nested `change` events where Backbone would normally recursively trigger the event while collecting all the changed attributes. With `sparse` enabled, the `changedAttributes` and `hasChanged` methods will only consider the attributes that have been change by the most recent call to `set`.

Actual use case of mine: I'm writing a layout engine backed by `Backbone.Model`. Many of the attributes depend on the value of others (think `width` depends on `leftGuide` and `rightGuide`). This means if one attribute is changed, many others may have to be recomputed. This dependency chain can get multiple levels deep, as some attributes depend on other computed attributes up until at the top where there are only known or root properties (think `leftGuide` depends on `guides` and `viewportWidth`).

The way Backbone currently does the `change` event the layout engine recomputes the same values multiple times, since `changedAttributes()` will get bigger and bigger and always contain all the properties that changed since the beginning of the chain.

As you can see the diff is very simple and I was able to implement this in my subclass by overriding `set`. But I thought others might benefit from it. There's also a Stackoverflow question I created yesterday with some discussion http://stackoverflow.com/questions/26979021/backbone-model-how-to-listen-for-changes-on-multiple-attributes-only-once/26994282#26994282