documentcloud/backbone — pull request #2636 by etler (2013-06-25 22:47:37+00:00)
Title: Give model.set a parse option

Implementing #2627

To get the parse option into `model.set` I also created `options.defaults` so the defaults can be applied after parsing. I'm not sure if we would want to support that, although I can see a use case for wanting to provide fallback values after creating a model the first time.

To prevent `options.defaults` from causing the side effect of re-applying defaults on merge in the `collection.set` method, I opted to extend the model constructor options when adding the defaults option. As a result of this, `_prepareModel` can no longer rely on the options side effect from `_validate` to access `options.validationError`, so I repopulate it instead. I think it's best to remove options side effects where possible as they're acting as a way to return extra data from a function, and as a result adds complexity to nested method calls and to the flow of data.

By moving parsing into `model.set`, I was also able to remove the `options._attrs` side effect hack for merging models. `options._attrs` was being used to return the post parsed attributes to `collection.set` for merging, but now that parsing is done within `model.set`, that is no longer necessary, and we can just send `model.set` the original copy of the attributes, and have it re-parse them there.

It would be better to find a way to do this without `options.defaults`, as that is out of the scope of this issue, but by doing things this way I was able to remove `options._attrs`, which to me seems worse than having `options.defaults`.