[英]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.