在以下代码中,我尝试使用动态需求触发事件。出于某种原因,我无法在eventRouter方法中访问app对象。我得到" TypeError:app未定义"错误。我已在各个控制器文件中实现了show事件的监听器。
我的问题类似于 this帖子,除了我的听众位于不同的控制器文件中,我无法按照帖子中的建议访问应用对象。
帮助赞赏!!!!
define(["app",
"tpl!templates/nav/nav.tpl",
"tpl!templates/nav/navMenuItem.tpl",
"entities/navEntity"
],
function(app, navTpl, navMenuItem, navEntity){
navMenuItem = Backbone.Marionette.ItemView.extend({
template: navMenuItem,
events: {
"click a": "eventRouter"
},
eventRouter:function(ev)
{
var that = this;
var moduleName = $(ev.currentTarget).text().toLowerCase();
require(['controllers/' + moduleName + 'Controller'], function(controller){
app.trigger(moduleName + ':show');
});
}
});
navMenu = Backbone.Marionette.CollectionView.extend({
tagName: 'ul',
itemView: navMenuItem,
collection: navEntity.navItems,
});
return {
navMenu: navMenu,
navMenuItem: navMenuItem
}
});
答案 0 :(得分:0)
要克服循环依赖关系,您可以查看以下内容:
https://stackoverflow.com/a/4881496/2303999
相应地管理您的模块并避免依赖。为您现在使用的函数创建公共js文件。您甚至可以使用Marionette Vent对象传递事件并根据该事件进行操作。