nilbus/Backbone.dualStorage — pull request #55 by ilkerde (2013-10-05 19:08:08+00:00)
Title: Consistent localsync invocation with backbone models

### Background
As a follow-up (better: addendum) on issue / PR #51, this change forces `localsync` to be called with backbone model or collection objects only.

### Solution
First, a guard to `localsync` was added, similar as proposed by nilbus [here](https://github.com/nilbus/Backbone.dualStorage/pull/51#issuecomment-25201923). The model type restriction is not enfored for the localsync specific extra methods (clear, hasDirtyOrDestroyed), since they don't use the parameter at all. The guard was developed test-first to ensure correct behavior.

Further on, all calls to localsync from dualstorage have been revisited. The 'read', 'create' and 'update' methods now update the model through its `set` method before the model is passed on to localsync (instead of the received object). The changes have been verified through tests. The `model.set` operations on models prior passing them to localsync have been refactored to a new helper function `mergeModelWithRemoteResponse` which updates the model with the remote object properties and returns it back to the caller.

To ensure that during update no 'change' events are triggered by backbone, the `silent:true` option is passed along the set call. As of Backbone 0.9.10, `silent` option truly suppresses the change event (see [Changelog](http://backbonejs.org/#changelog)). All refactorings were accompanied with rigid unit tests.

### Motivation

Mainly to finally get the test suite all green back again. Apart from that, it was interesting to see that chewing on a single fix with serious code review can reveal other rooms for improvement. Thanks @nilbus for reviewing code with care.

### Notes

Whilst this PR concentrates on the localsync consistency (as a follow-up on PR #51), it also might be a good starting point to fix issue #53, since now all model merges are handled in single function `mergeModelWithRemoteResponse`. A possible solution path for #53 would be to use `parseBeforeLocalSync` there.

Hope now localsync and custom id attribute handling is fixed. Please, review with care. Feedback always welcome.