Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve returned xhr(promise) with model. #2598

Closed
wants to merge 1 commit into from

Conversation

tomalec
Copy link

@tomalec tomalec commented Jun 9, 2013

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.

function doSmthWithModel( model ){
    console.log(model);
    //..
}
//jQuery.Deferred
model.save().then( doSmthWithModel );

to work around false returned by Model#save

$.when( model.save() ).then( doSmthWithModel )

or use Q, which also solves false issue

Q( model.save() ).then( doSmthWithModel )

if 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.

@jashkenas
Copy link
Owner

This is a bit too piecemeal of an approach to be merged as-is ... although thoughts on having nicer promise workflows are certainly welcome.

It would also break a lot of apps.

@jashkenas jashkenas closed this Sep 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants