Marionette - 监听从 collection 触发的自定义事件
Marionette - listenTo a custom event triggered from a collection
我正在使用事件聚合器 (EA) 来跟踪我的应用程序中的所有事件。但是有一种情况我无法让 EA 监听从 collection 触发的自定义事件。请参阅下面的更多详细信息。
eMgr (事件管理器,即事件聚合器):
define(['backbone.wreqr'],function(Wreqr){
"use strict";
return new Wreqr.EventAggregator();
})
collection:
define([ 'underscore', 'backbone', 'eMgr', 'models/MainMenu/MM.model.category'], function(_, Backbone, eMgr, m_Category){
var CategoriesCollection = Backbone.Collection.extend({
model: m_Category,
initialize: function(options) {
console.log('CategoriesCollectionView initialized...');
this.url= 'test1';
this.fetch().then(function(){
eMgr.trigger("test1")
})
}
});
return CategoriesCollection;
});
布局视图:
define([ 'backbone', 'underscore', 'marionette', 'eMgr',
'templates/template.mainmenu',
'collections/MainMenu/MM.collection.categories',
'layouts/MainMenu/collectionview.categories' ],
function (Backbone, _, Marionette, eMgr, template, c_Categories, cv_Categories) {
"use strict";
var CategoryLayoutView = Marionette.LayoutView.extend({
template: template['categories.layout'],
regions: {
categories : '#categories'
},
id: 'categories-layout',
initialize: function(options){
var r_categories = this.categories;
this.listenTo(eMgr, "test1", this.test);
this.categories = new c_Categories(options)
},
test: function(){
console.log("test");
var categories_layout = new cv_Categories({ collection: this.categories});
categories_layout.render()
},
onRender: function(){
},
onShow: function(){
}
});
return CategoryLayoutView;
});
正如您在上面看到的,我想要实现的是在 collection 完成加载所有数据后呈现 CollectionView (cv_Categories)。
但是它不起作用,除非我按如下方式收听事件:
eMgr.listenTo(eMgr, "test1", this.test);
这将触发该功能,尽管我无法再访问我的 LayoutView 的 methods/variables 等等。所以我无法访问我的 collection,因此无法正确启动我的 CollectionView。
现在的问题是,我做错了什么还是一切都按预期进行?还有其他方法可以做到这一点吗?
您可以 eMgr.on("test1", this.test, this)
以 this
作为上下文执行 this.test
(注意第三个参数)。这样您就可以保持对视图 "this" 范围的访问,并且因此对于您的视图的功能。
如果您更喜欢使用 listenTo,那么您可以 eMgr.listenTo(eMgr, "test1", this.test.bind(this))
。不过,我发现它更加冗余和不必要的冗长。
我正在使用事件聚合器 (EA) 来跟踪我的应用程序中的所有事件。但是有一种情况我无法让 EA 监听从 collection 触发的自定义事件。请参阅下面的更多详细信息。
eMgr (事件管理器,即事件聚合器):
define(['backbone.wreqr'],function(Wreqr){
"use strict";
return new Wreqr.EventAggregator();
})
collection:
define([ 'underscore', 'backbone', 'eMgr', 'models/MainMenu/MM.model.category'], function(_, Backbone, eMgr, m_Category){
var CategoriesCollection = Backbone.Collection.extend({
model: m_Category,
initialize: function(options) {
console.log('CategoriesCollectionView initialized...');
this.url= 'test1';
this.fetch().then(function(){
eMgr.trigger("test1")
})
}
});
return CategoriesCollection;
});
布局视图:
define([ 'backbone', 'underscore', 'marionette', 'eMgr',
'templates/template.mainmenu',
'collections/MainMenu/MM.collection.categories',
'layouts/MainMenu/collectionview.categories' ],
function (Backbone, _, Marionette, eMgr, template, c_Categories, cv_Categories) {
"use strict";
var CategoryLayoutView = Marionette.LayoutView.extend({
template: template['categories.layout'],
regions: {
categories : '#categories'
},
id: 'categories-layout',
initialize: function(options){
var r_categories = this.categories;
this.listenTo(eMgr, "test1", this.test);
this.categories = new c_Categories(options)
},
test: function(){
console.log("test");
var categories_layout = new cv_Categories({ collection: this.categories});
categories_layout.render()
},
onRender: function(){
},
onShow: function(){
}
});
return CategoryLayoutView;
});
正如您在上面看到的,我想要实现的是在 collection 完成加载所有数据后呈现 CollectionView (cv_Categories)。
但是它不起作用,除非我按如下方式收听事件:
eMgr.listenTo(eMgr, "test1", this.test);
这将触发该功能,尽管我无法再访问我的 LayoutView 的 methods/variables 等等。所以我无法访问我的 collection,因此无法正确启动我的 CollectionView。
现在的问题是,我做错了什么还是一切都按预期进行?还有其他方法可以做到这一点吗?
您可以 eMgr.on("test1", this.test, this)
以 this
作为上下文执行 this.test
(注意第三个参数)。这样您就可以保持对视图 "this" 范围的访问,并且因此对于您的视图的功能。
如果您更喜欢使用 listenTo,那么您可以 eMgr.listenTo(eMgr, "test1", this.test.bind(this))
。不过,我发现它更加冗余和不必要的冗长。