我试图让我的路由器决定只显示登录主页视图,否则显示登录视图或注册表视图。
我的应用程序最初位于localhost:8080/indexapp。
我在data-main文件中加载它。但我认为,与做出这一决定的行动相关的路线并未触发。
是否有可能在加载的页面上具有要触发的路由的路由器。
另外,我有一行注释,说明导致未定义的this.loginModel.fetch();
不是函数错误。
define([
'jquery',
'ratchet',
'underscore',
'backbone',
'login/loginview',
'login/loginmodel',
'register/registerview',
'home/homeview',
],
function($, Ratchet, _, Backbone, LoginModel, LoginView, RegisterView, HomeView){
var MainRouter = Backbone.Router.extend({
routes: {
"": "showHome",
//"/": "showHome",
"login": "showLogin",
"register": "showRegister"
},
initialize: function(){
Backbone.history.navigate("home", {trigger:true})
},
showHome: function(){
//var self = this, loginModel = new LoginModel();
this.loginModel = new LoginModel();
//this.loginModel.fetch();
if(this.loginModel.get('loginp') == true ){
this.homeView = new HomeView();
this.homeView.render();
}
else {
Backbone.history.navigate("/login", {trigger:true})
}
/*
this.loginModel.fetch({
success: function(model){
if( model.get('loginp') == true ){ //show you app view for logged in users!
this.homeView = new HomeView();
this.homeView.render();
}
else { //not logged in!
Backbone.history.navigate("/login", {trigger:true})
}
},
});
*/
},
showLogin: function(){ //display your login view
this.loginView = new LoginView;
//this.loginView.fetch();
this.loginView.render();
},
showRegister: function(){ //display your register view
this.registerView = new RegisterView;
//this.registerView.fetch();
this.registerView.render();
},
});
return MainRouter;
});
登录模型:
define([
'underscore',
'backbone',
],
function(_, Backbone) {
var LoginModel = Backbone.Model.extend({
urlRoot: '/login',
initialize: function(){
this.fetch();
},
defaults: {
username: null,
},
});
return LoginModel;
});
登录视图:
define([
'jquery',
'ratchet',
'underscore',
'backbone',
'login/loginmodel',
'text!login/logintemplate.html',
],
function($, Ratchet, _, Backbone, LoginModel, LoginTemplate){
var LoginView = Backbone.View.extend({
el: $('body'),
model: new LoginModel,
/*
initialize: function(){
this.model = new LoginModel;
},
*/
template: _.template( LoginTemplate ),
render: function(){ //display your login view
this.$el.html( template( this.model.attributes ) );
},
});
return LoginView;
});
发布于 2014-10-16 22:49:53
很高兴看到您采纳了我的想法!;)
惟一缺少的是在$(document).ready上使用Backbone.history.start()启动路由。
http://backbonejs.org/#Router
它是这样工作的:您需要在调用Backbone.history.start()之前实例化您的路由器。
因此,在主应用程序文件中,需要您的MainRouter,创建一个实例并启动历史记录。
$(document).ready(function(){
new MainRouter();
Backbone.history.start();
});
https://stackoverflow.com/questions/26407271
复制相似问题