主干中没有标准方法可以做到这一点,需要您修改模型和集合。我知道你没有在问题中提到 jQuery,我只是在我提供的延迟对象功能的示例中使用它,并且可以换出任何延迟对象库。
这种方法的问题是集合get() 方法必须返回一个延迟对象供您使用,而不是模型。这是在集合get() 方法中构建异步操作的不幸产物。
对于模型,我们需要一种方法来跟踪它是否被获取。为此使用时间戳可能允许您“超时”模型并在它已过期时重新获取它。为此,我们覆盖模型上的 fetch 方法。
警告:此代码未经测试,但提供了解决此问题的方法
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 方法以包含模型获取例程。 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);
});
这将允许您使用您在问题中提供的工作流程。