Sync issue in collection fetch backbone

Posted by Stefano Maglione on Stack Overflow See other posts from Stack Overflow or by Stefano Maglione
Published on 2013-08-02T15:33:19Z Indexed on 2013/08/02 15:36 UTC
Read the original article Hit count: 129

Filed under:
|

i'm fetching a collection but i've problem because into the collection the function parse use an another ajax call.So if i try to console.log the response of fetch after the fetch linecode but it is ever undefined.

Function call fetch:

  friends: function(){

    var amici=new Amicizie();
    var amicilist=amici.fetch();


    console.log(amicilist);<---undefined,because executed before  fetch

collection called:

    var obj={};

    var Amicizie = Backbone.Collection.extend({


    url:'https://api.parse.com/1/classes/User/',

    parse: function(data) {

    var cur_user=Parse.User.current().id;

    $.ajax({

    type: 'GET',

    headers: {'X-Parse-Application-Id':'qS0KL***EM1tyhM9EEPiTS3VMk','X-Parse-REST-API- 
    Key':'nh3eoUo9G***JIfIt1Gm'},

    url: "https://api.parse.com/1/classes/_User/?where=....",

    success: function(object) {
      console.log(object );
      obj=object;
      console.log(obj );

    },
    error: function(data) {

      console.log("ko" );
    }





    });

    return obj.results;

}


});

return Amicizie;

});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about backbone.js