当获取骨干收集和创建XHR对象之间的关联 [英] Association between Backbone Collection and XHR object created when fetching
问题描述
有一个骨干集合,当你执行创建XHR对象之间的现有关联一个 .fetch()
就可以了?
Is there an existing association between a Backbone Collection and the XHR object created when you execute a .fetch()
on it?
我愿做这样的事情:
collection = new Backbone.Collection;
xhrObj = collection.fetch();
xhrObj.parent == collection; //true
这个更大的目标是为我检查,看看是否有任何挂起 .fetch()
的一个特定集合。如果有另一种方式在骨干做到这一点,请让我知道。我想我会只存储XHR对象,并检查是否任何没有完成与该集合关联的那些的。
The larger goal is for me to check to see if there are any pending .fetch()
's for a specific collection. If there is another way to do this in Backbone, please let me know. I figured I would just store XHR objects and check if any of the ones that have not finished are associated with the collection.
推荐答案
您可以做到这一点是这样的:
You can do it like this :
var fetch = Backbone.Collection.prototype.fetch;
Backbone.Collection.prototype.fetch = function (options) {
options = options ? _.clone(options) : {};
var success = options.success;
options.success = function (collection, resp, options) {
collection.xhrObj = resp;
if (success) success(collection, resp, options);
};
var error = options.error;
options.error = function (collection, resp, options) {
collection.xhrObj = void 0; // or whatever you want
if (error) error(collection, resp, options);
};
fetch.call(this, options);
};
之后,如果取调用成功,你可以找到你的收藏中的服务器响应。
after that, if the fetch call succeeded you can find the server response in your collection.
这篇关于当获取骨干收集和创建XHR对象之间的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!