我试图通过以下方式跟踪路线的变化:
define([
'jquery',
'underscore',
'backbone',
'router',
'text!templates/sidebar.html'],
function($, _, Backbone, Router, SidebarTemplate) {
var SidebarView = Backbone.View.extend({
el: $('#sidebar'),
initialize: function() {
_.bindAll(this, 'render');
this.listenTo(Router, 'route', this.routeChanged, this);
},
events: {
'click a' : 'navigated'
},
render: function() {
var cTem = _.template(SidebarTemplate);
this.$el.html(cTem);
},
routeChanged: function(router, route) {
console.log(route);
},
navigated: function(a) {
this.$('li').removeClass();
$(a.currentTarget).parent().addClass('active')
}
});
return SidebarView;
});
我得到了:
Uncaught TypeError: Cannot read property '_listenId' of undefined
在这一行:
this.listenTo(Router, 'route', this.routeChanged, this);
我认为这是因为存在循环依赖。确实如此吗?我该如何解决这个问题?
router.js的相关来源:
define([
'jquery',
'underscore',
'backbone',
'views/dashboard',
'views/users',
'views/venues',
'views/payments',
'views/header',
'views/sidebar',
'views/signup',
'views/login'],
function($, _, Backbone, DashboardView, UsersView, VenuesView, PaymentsView, HeaderView, SidebarView, SignupView, LoginView) {
var Router = Backbone.Router.extend({
initialize: function() {
self.sidebarView = new SidebarView();
self.headerView = new HeaderView();
self.sidebarView.render();
self.headerView.render();
},
routes: {
'': 'showDashbaord',
'dashboard': 'showDashbaord',
'users': 'showUsers',
'venues': 'showVenues',
'payments': 'showPayments',
'login': 'showLogin',
'signup': 'showSignup',
'logout': 'logout'
}
});
return new Router();
});
谢谢!
答案 0 :(得分:0)
define([
'require',
'jquery',
'underscore',
'backbone',
'router',
'text!templates/sidebar.html'],
function(require, $, _, Backbone, Router, SidebarTemplate) {
return function () {
Router = require("router");
然后在使用其他模块的SidebarView时,您需要:
var SideBarViewInst = SideBarView()
这将延迟导入,直到加载所有模块。