documentcloud/backbone — pull request #2221 by tgriesser (2013-01-30 23:06:40+00:00)
Title: Making error/success handlers promise compatible

I don't think I made a solid enough argument in [my last request](https://github.com/documentcloud/backbone/pull/2173) for reverting the changes in #2003 and 1f3f45252f

The [Promises](http://promises-aplus.github.com/promises-spec/) spec provides one of the best approaches to handle async calls, such as is needed with data syncing - (jQuery's ajax uses an [almost Promise/A compatible](https://github.com/jquery/jquery/commit/a41f2406748e3113751ab1e5b5d990d9144123fc) deferred object internally).

The initial request of #2003 was to standardize the arguments passed back to the success/error handlers, but the merged request ended up requiring that the options handlers pass the model and the options through the sync implementation.

While the changes in 1f3f45252f aimed to make the code a little dry-er, it pushed the success/error handling logic into `Backbone.sync`. This then *requires* that the developer replace `Backbone.sync` and also replicate this logic if they wish to use their own syncing mechanism. While the change cuts a few lines internally, moving this logic out of the model/collection methods and into a separate component makes the `Backbone.sync` more tightly coupled with the models/collections. This pull request puts the logic back into the model/collection methods, making the success/error handlers suitable for use as stand-alone deferred `resolve` or `reject` handlers, respectively.

Here's an example of what can be accomplished when the previous (0.9.2 and earlier) sync structure is in place using (q.js)[https://github.com/kriskowal/q], a great library for implementing promises:

```html

// ...

```

The same behavior would require much more work to implement when needing to reproduce the `success`/`error` triggers, and passing through the `model` & `options`.

Unlike the other request, this does not re-attach `wrapError` as a property on `Backbone` (keeps the global namespace clean), and it fulfills the original intent of #2003 which was to standardize the Backbone success/error handlers with `(model, resp, options)`.