我下载并检查了您的代码。以下可能是问题:
require.js
仅适用于AMDs
. 由于骨干不再支持AMD
. 您将需要使用AMD
已启用的Backbone
. 你可以在这里得到
TestView 是您路由器中的依赖项。所以它在路由器加载之前加载。
您可能想要改进编码模式。这是我的建议:
应用程序.js
define([
'backbone', 'router', ], function(Backbone, MainRouter){ 'use strict';
var AppView = Backbone.View.extend({
initialize: function(){
App.router = new MainRouter();
Backbone.history.start();
}
});
return AppView;
});
路由器.js
define([
'backbone',
'view/TestView'
], function(Backbone, TestView){
var Main = Backbone.Router.extend({
routes: {
'test': 'test'
},
test: function(){
new TestView({
// pass model or collection to the view
// model: new TestModel // remember to require
});
}
});
return Main;
});
编辑
监听事件:
// in main.js
var window.Vent = {};
_.extend(window.Vent, Backbone.Events);
// now in any view you can trigger a event
$('something').on('click', function(){
window.Vent.trigger('somethinghappened', this);
// this is reference to current object
});
// now in other view you can do
window.Vent.on('somethinghappened', this.run, this);
// this in the end is the reference we passed when event was triggered
run: function(obj){
//this function will run when the event is triggered
// obj is the object who triggered the event
}
PS:为什么要在view中使用router??我已经构建了很多主干应用程序。从来不需要这样做。