Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Local fetch() has no complete() callback. #27

Closed
Rovanion opened this issue Nov 21, 2012 · 6 comments
Closed

Local fetch() has no complete() callback. #27

Rovanion opened this issue Nov 21, 2012 · 6 comments

Comments

@Rovanion
Copy link

With the fallowing example-code with the local attribute set to true:

var Favourites = Backbone.Model.extend({
    local: true, //Only sync against LocalStorage                                                                           
    url: 'favourites',
    initialize: function(){
        var self = this;
        this.fetch().complete(function(){
            console.debug(self);
        });
    },

this.fetch().complete() is not defined. This deferred call is a function from the jQuery ajax() function. This usage was recommended to me in this Backbone bug: jashkenas/backbone#423

@Rovanion
Copy link
Author

Neither this variant seems to be working:

var Favourites = Backbone.Model.extend({
    local: true, //Only sync against LocalStorage                                                                             
    url: 'favourites',
    initialize: function(){
        var self = this;
        this.fetch({
            success: function(){
                console.debug(self);
            }
        });
    },
});

@lucian1900
Copy link
Collaborator

I would expect the second to work. If it doesn't, that's a bug.

@nilbus
Copy link
Owner

nilbus commented Nov 21, 2012

Since for localstorage there's no XMLHttpRequest to pass back, you're not going to be able to hook in complete() like in your first example. The success callback is what you want.

I'm surprised that your second example didn't work. I have several tests in my fork that test both the local: true option and the success callback. This spec relies on the fetch success callback for the test to pass. I tried it with local: true on the model instead of remote: false in the fetch call, which also worked. I also rely on this fetch success callback in one of my apps that uses this plugin.

@nilbus
Copy link
Owner

nilbus commented Nov 21, 2012

I put together a jsfiddle with your code, and it seems to work. Run this, and you see the debug output in the console:

http://jsfiddle.net/nilbus/P3euQ/

@Rovanion
Copy link
Author

Yeah that seems to work. Odd thing that I couldn't get it working in my code then. I'll close this off then.

@reubano
Copy link

reubano commented Jan 31, 2014

If you have jquery, you can wrap the fetch in a deferred.

dfdFetch: =>
  $.Deferred((deferred) => @fetch
    success: deferred.resolve
    error: deferred.reject).promise()

And then you can do things like @dfdFetch().done(somefunc).fail(otherfunc)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants