我有一个集合(使用Backbone.paginator ),它从服务器获取一组模型并返回给我。这个集合在我的“主视图”和“子视图”上使用。在每个视图中,我都有一个on("change")事件,该事件在每个视图中执行特定的操作。我在想,我是否可以监听一些"start fetch“事件(类似于jquery上的beforeLoad )来添加一个加载器gif。Backbone提供这样的功能吗?
如果不是..。我如何扩展它?
谢谢!
发布于 2012-10-04 21:54:20
您可以像这样扩展Backbone Collection原型:
(function() {
var fetch = Backbone.Collection.prototype.fetch;
Backbone.Collection.prototype.fetch = function() {
this.trigger('beforeFetch');
return fetch.apply(this, arguments);
};
})();现在,您可以执行以下操作:
myCollection.on('beforeFetch', function() {
// take care of before fetch business
});发布于 2012-12-15 16:05:30
只要使用"request“事件即可。它在模型(或集合)启动对服务器的请求时触发。它从0.9.9版本开始可用。
发布于 2012-10-04 21:56:14
您只需在每次fetch调用之前添加一个对设置加载屏幕的方法的调用:
this.collection.on('reset', this.displayNormal)
this.displayLoader();
this.collection.fetch();这应该可以让你开始学习了。如果您需要该事件(我可以理解您为什么需要),您可以像这样覆盖fetch
fetch: function() {
this.trigger('beforeFetch');
return Backbone.Collection.prototype.fetch.apply(this, arguments);
}希望这能有所帮助!
https://stackoverflow.com/questions/12728706
复制相似问题