我想要类似以下的行为:
- 对集合调用 get()。
- 仅在尚未获取模型时获取模型。
- 返回模型或触发表示模型已准备就绪的事件。
在 Backbone 中是否有标准的方法来做到这一点?
最佳答案
backbone 中没有标准方法可以做到这一点,需要您修改模型和集合。我知道您没有在问题中提到 jQuery,我只是在示例中使用它来提供其延迟对象功能,并且可以换出任何延迟对象库。
此方法的问题是集合 get()
方法必须返回一个延迟对象供您使用,而不是模型。这是在集合 get()
方法中构建异步操作的不幸副产品。
对于模型,我们需要一种跟踪它是否被提取的方法。为此使用时间戳可能允许您“超时”模型并在它已过期时重新获取它。为此,我们重写了模型上的获取方法。
警告:此代码未经测试,但提供了一种解决此问题的方法
var Model = Backbone.Model.extend({
/**
* Last fetched public variable
* @var {Object} Date || null
*/
lastFetched : null,
/**
* Custom fetch always adds a timestamp to the model
* when it was last fetched from the server. This
* allows us to prevent unnecessary calls to the backend
* when a model is still fresh.
* @return {Object} jQuery deferred
*/
fetch : function() {
if ( _.isNull(this.lastFetched) ) {
this.lastFetched = new Date().getTime();
return Backbone.Model.prototype.fetch.apply(this, arguments);
} else {
return new $.Deferred().resolve();
}
}
});
集合需要覆盖其获取方法以包含模型获取例程。 get 方法不会返回模型,而是返回一个延迟对象,该对象可用于在模型准备就绪时链接回调。
var Collection = Backbone.Collection.extend({
model : Model,
/**
* Custom get retrieves a model if it exists and returns the result
* of the models custom fetch method
* @param {Integer} id | cid
* @return {Object}
*/
get : function(id) {
var model = Backbone.Collection.prototype.get.apply(this, args);
if ( ! model )
model = new this.model({id : id});
return {
deferred : model.fetch(),
model : model
};
}
});
用法
var collection = new Collection(),
result = collection.get(1);
result.deferred.done(function() {
console.log(result.model);
});
这将允许您使用您在问题中提供的工作流程。
关于javascript - 根据需要在 Backbone Collection 中获取模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25536704/