json 如何让 Backbone.ajax 成功返回数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15980586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:40:54  来源:igfitidea点击:

How to get Backbone.ajax to return data on success

javascriptjsonbackbone.js

提问by Jason

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

我试图让 Backbone.ajax 返回集合“集合”。我需要程序的另一部分中的模型。

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

我想让数据在与 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){ val
        var Model = Backbone.Model.extend({});
        var Collection = Backbone.Collection.extend({
            model:Model
        });
        collection = new Collection(val);
        console.log(collection);
    }
});

回答by JayC

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 resetor addthe 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 avoidby using Backbone.

不!您的连接的存在几乎不应该取决于任何“ajax”调用!您需要将集合的定义和实例移到 ajax 成功方法之外和 ajax 调用之前的某个位置,然后只是resetadd成功方法中的集合或类似的东西。您需要它外部,以便您可以在实际需要数据之前定义所有视图绑定等;否则你最终会遇到一个大混乱——你试图通过使用 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);
    }
});

回答by hexacyanide

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.

当函数被执行时,它将等待回调执行函数内部的任何内容。因此,我建议您为 AJAX 调用失败的情况添加回调。