Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  69] [ 7]  / answers: 1 / hits: 8680  / 10 Years ago, wed, april 9, 2014, 12:00:00

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



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;

});

More From » jquery

 Answers
8

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


[#46166] Tuesday, April 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaylea

Total Points: 459
Total Questions: 97
Total Answers: 106

Location: Denmark
Member since Wed, Aug 26, 2020
4 Years ago
;