How to get Backbone.ajax to return success data

I am trying to return Backbone.ajax to return the collection "collection". I need a model in another part of the program.

I would like to make the data available at the same level as the ajax method.

Backbone.ajax({
    dataType: "jsonp",
    url: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=25",
    data: "",
    success: function(val){ val
        var Model = Backbone.Model.extend({});
        var Collection = Backbone.Collection.extend({
            model:Model
        });
        collection = new Collection(val);
        console.log(collection);
    }
});
+5
source share
2 answers

Noooo! "ajax"! ajax - ajax, reset add - . , .., ; - , , Backbone.

//definitions
var MyModel = Backbone.Model.extend({});
var MyCollection = Backbone.Collection.extend({
    model:Model
});

//wherever you need a collection instance
collection = new MyCollection();

//wherever you need to do the ajax
Backbone.ajax({
    dataType: "jsonp",
    url: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=25",
    data: "",
    success: function(val){
        collection.add(val);  //or reset
        console.log(collection);
    }
});
+12

:

function ajaxCall(callback) {
  Backbone.ajax({
    dataType: "jsonp",
    url: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=twitterapi&count=25",
    data: "",
    success: function (val) {
      var Model = Backbone.Model.extend({});
      var Collection = Backbone.Collection.extend({
        model: Model
      });
      collection = new Collection(val);
      callback(collection);
    }
  });
}

ajaxcall(function (collection) {
  //do something with the collection when the callback is returned
});

, , . , AJAX .

+3

All Articles