5

The following example code works well:

Auth_controller.prototype.isLogged = function(){ 
    //Check if the user is authenticated 
    var getAuthStatus = this.auth_model.fetch(); 
    return getAuthStatus; 
}; 

Auth_controller.prototype.redirect = function(fragment, args, next){ 

    var getAuthStatus = this.isLogged(); 
    var self = this; 

    $.when(getAuthStatus).then(function(response){ 
        //Do something with the response 
    }
}); 

This doesn't seem to work for a Collection though.
When I console log the collection, I get an empty collection back.

I know that I can use the success callback function from within the method (tested that already), but I don't want to do that, because I want the function to return a promise that I can call from other functions as well.
Edit -> No, sorry it doesn't work in the success callback either so it seems.

Any suggestions for a workaround ?

Edit;

This image shows what is returned from the model and collection fetch methods.
Unless I'm doing something wrong that is obvious, I don't understand why this happens.
When console logging the returned response in the success callback, I see that the empty object as shown in the screenshot, gets populated.

enter image description here

Edit2:

This is what my collection looks like:

define([
  /*--- libraries ---*/
  'jquery',     
  'underscore', 
  'backbone', 

  /*--- model ---*/
  'models/users/role_model'

], function($, _, Backbone, 
                Role_model){

    var Role_collection = Backbone.Collection.extend({ 
        url: '/ingeb/api_v1/users/roles', 
        model: Role_model 
    }); 

    return Role_collection; 

}); 
html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • Where exactly do you get an empty collection, and in what callback do you see that it should contain some items actually? – Bergi Apr 09 '14 at 15:34
  • I have added a screenshot. In the jQuery when - then callback. It works well for the model, but when using the same method for the collection, it doesn't work... – html_programmer Apr 09 '14 at 15:44
  • 1
    This is what Collection.prototype.fetch returns: `return this.sync('read', this, options);` https://github.com/jashkenas/backbone/blob/master/backbone.js#L885-L898 in turn, sync does `return Backbone.sync.apply(this, arguments);` in turn which returns a promise. Is it possible you've overwritten .sync for this particular collection? Can you show us the code for that collection? – Benjamin Gruenbaum Apr 09 '14 at 15:56

1 Answers1

10

Actually, the collection's fetch does return a promise:

Delegates to Backbone.sync under the covers for custom persistence strategies and returns a jqXHR.

See http://backbonejs.org/#Collection-fetch and http://api.jquery.com/jQuery.ajax/#jqXHR

David Sulc
  • 25,946
  • 3
  • 52
  • 54
  • Do you know what is the reason that the collection.fetch() method returns to me an object that looks like the screenshot I have added ? – html_programmer Apr 09 '14 at 15:46
  • 2
    @KimGysen you've either overridden `.fetch` or `.sync` on that collection, or `Backbone.ajax`. – Benjamin Gruenbaum Apr 09 '14 at 15:57
  • I suppose it's not related to the Backbone.ajax... otherwise I guess that the model.fetch wouldn't work well either(?). Can you give me a guideline on how I could locate the issue? – html_programmer Apr 09 '14 at 16:09
  • Did something stupid -> I forgot to assign the promise to a variable before returning it, and returned the collection itself instead. It was a foolish syntactic error that I made. – html_programmer Apr 09 '14 at 16:30
  • 1
    Yeah, if you pass a non-deferred value to (e.g.) `$.when()` it will execute the success callbacks immediately. – David Sulc Apr 10 '14 at 06:14