我开始关注Organizing your application using Modules (require.js教程,但在将第一个事件处理程序添加到我的视图后 - 我遇到了问题:
// Filename: views/project/list
define([
'jquery',
'underscore',
'backbone',
'handlebars',
'collections/projects',
'text!templates/projects/list.js'
], function ($, _, Backbone, Handlebars, ProjectsCollection, projectListTemplate) {
var ProjectListView = Backbone.View.extend({
el: $('#container'),
events: {
"click .open-proj": "openProject",
},
initialize: function () {
...
},
render: function () {
...
},
openProject: function(e) {
// HERE I WANT TO TRIGGER ROUTING VIA router.navigate
alert("opened");
}
});
// Our module now returns our view
return ProjectListView;
});
在openProject
回调中我想触发路由,但是我不能向app.js引入依赖,因为它会导致循环依赖(路由器依赖于视图)。我该怎么办呢?
答案 0 :(得分:3)
您可以在创建路由器时将其路由到ProjectListView
:
var projectListView = new ProjectListView({
router: app_router
});
由于ProjectListView
是Backbone.View
,它可以使用this.options.router
访问路由器,此处没有循环依赖问题。