我正在使用requirejs和backbone开发应用程序,在索引视图(索引页面)中我遇到了2个困难...
当我使用这样的导航功能时:
this.navigate( “仪表板/”);
如果我使用的话,它不起作用:
Backbone.history.navigate("dashBoard/");
它工作正常。即使在我的“路线”中,我也没有触发功能。这是我的索引视图..
define(["singleton","backbone"],function (obj,Backbone) {
window.EDMS = obj || {};
EDMS.index = Backbone.View.extend({
el:$(".loginPage > section"),
events:{
"click #loginBtn" : "enter"
},
initialize:function(){
this.$el.html(this.template());
},
enter:function(e){
e.preventDefault();
var userName = $.trim(this.$el.find("#userName").val()),
password = $.trim(this.$el.find("#password").val());
if(userName && password){
Backbone.history.navigate("dashBoard/"); //only this is works
//this.navigate("dashBoard/") not working.
}else{
return false;
}
}
});
return EDMS.index;
})
但是对于“EDMS.routers”的控制台 - 我正在获取方法...如何正确地将我的索引视图同步到路由器..任何帮助?
我的routers.js
define(["singleton","backbone","utils"], function (EDMS,Backbone,utils) {
window.EDMS = window.EDMS || {};
EDMS.routers = Backbone.Router.extend({
routes:{
"":"caller",
"dashBoard/":"dashBoard"
},
initialize:function(){
utils(["index"]);
},
caller:function(){
console.log("i am called");
},
dashBoard:function(){
console.log("welcome to dashBoard");
}
});
return EDMS.routers;
})
我的应用初始化在require.js
require(["jquery","underscore","backbone","singleton","routers"],function ($,_,Backbone,EDMS,routers) {
EDMS.router = new EDMS.routers();
Backbone.history.start();
});
答案 0 :(得分:1)
使用requirejs
可以分离您的模块(您可能已经知道)。正确的方法是通过require
访问分离的模块。
当然你应该在你的应用程序中有这样的东西:
require.config({
paths: {
jquery: 'libs/jquery/jquery',
underscore: 'libs/underscore/underscore',
backbone: 'libs/backbone/backbone'
}
});
你的路由器在最终的应用程序初始化中被初始化,基本上代码应该是这样的:
define('routers', ["backbone","utils"], function (Backbone,utils) {
var routers = Backbone.Router.extend({
routes:{
"":"caller",
"dashBoard/":"dashBoard"
},
initialize:function(){
utils(["index"]);
},
caller:function(){
console.log("i am called");
},
dashBoard:function(){
console.log("welcome to dashBoard");
}
});
return new routers();
});
在您的视图中:
define('indexView', ["routers","backbone"],function (router,Backbone) {
var index = Backbone.View.extend({
el:$(".loginPage > section"),
events:{
"click #loginBtn" : "enter"
},
initialize:function(){
this.$el.html(this.template());
},
enter:function(e){
e.preventDefault();
var userName = $.trim(this.$el.find("#userName").val()),
password = $.trim(this.$el.find("#password").val());
if(userName && password){
router.navigate("dashBoard/");
}else{
return false;
}
}
});
return index;
});
在初始化
中require(["jquery","underscore","backbone","indexView","routers"],function ($,_,Backbone,indexView,routers) {
window.App = { indexView: new indexView() };
});
通过执行此操作,您将在路由器中弹出一个事件,您可以从中收集任何集合/视图/模型,如下所示:
router.on('route', function() { console.log('we have routed'); });