最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Backbone.ajax to return data on success - Stack Overflow

programmeradmin4浏览0评论

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

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

Backbone.ajax({
    dataType: "jsonp",
    url: ".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);
    }
});

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

I would like to make the data available on 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);
    }
});
Share Improve this question edited Apr 13, 2013 at 3:18 machineghost 35.8k32 gold badges173 silver badges258 bronze badges asked Apr 12, 2013 at 20:48 JasonJason 6942 gold badges11 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

Noooo! Your connection's existence should almost never be contingent on any "ajax" invocation! You need to move the collection's definition and instance outside of your ajax success method and somewhere before the ajax invocation, and then just reset or add the collection within the success method or something similar. You need it external so that you can define all of your view bindings, etc before you actually need the data; otherwise you end up with a big mess--one which you are trying to avoid by using 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);
    }
});

You can apply a callback to the function like so:

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
});

When the function is executed, it will wait for the callback to execute anything inside the function. Therefore, I'd suggest that you add a callback for the case in which the AJAX call fails.

发布评论

评论列表(0)

  1. 暂无评论