[英]Can an event be triggered in Backbone once a collection has been fetched and rendered?
fetch
collection.fetch([options])
[...] 选项哈希采用
success
和error
回调,它们将分别作为参数传递(集合,响应,选项)和(集合,xhr,选项)。
在success
的收集更新后, 打完所有的它会被称为回调将被称为"add"
事件已被触发和处理。 所以你可以这样做:
collection.fetch({
success: function(collection, response, options) {
collection.trigger('fetched_and_notified');
}
});
任何关心的人都可以collection.on('fetched_and_notified', ...)
来监听这样的事件。 如果您愿意,可以提供自己的fetch
实现来自动触发此事件,如下所示:
fetch: function(options) {
options = options ? _.clone(options) : { };
var success = options.success;
options.success = function(collection, response, options) {
collection.trigger('fetched_and_notified', collection);
if(success)
success(collection, response, options);
};
Backbone.Collection.prototype.fetch.call(this, options);
}
您可以根据应用程序使用Backbone.Events在所有类型的对象上触发自定义事件
要在渲染视图后触发事件,请:
var MyView = Backbone.View.extend({
initialize: function() {
_.bindAll(this);
},
render: function() {
//Do all the rendering
this.trigger('myEvent');
}
});
this.trigger('myEvent' )就是这个技巧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.