Resolve returned xhr(promise) with model. #2598
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
I have faced the need to use Promises with Backbone methods, or
Backbone.sync
in general.I would really like to use model, as first argument of fulfill function.
I read problems related to #1774, #2489, and #2345.
I come with a simple idea: instead of trying to introduce Promises into entire Backbone flow, we can tune those given by jQuery (if any).
Simply, if we have Promise available, we will use it to resolve with
model
, otherwise we will return xhr as we did before.Thanks to that, old non-promise apps and flows will still work, but we could use jQuery.Deferreds, Q, etc. to do what we want.
to work around
false
returned by Model#saveor use Q, which also solves
false
issueif we need a rejection even nicer
( model.save() || Q.reject(model))
So it will be tiny step towards Promises in Backbone.
I have also made it work for jQuery>1.6 with
.pipe
if we need @50c8cf49ac62ef8fa9a0a4169fa6f3dba89bd7d4.