Javascript 使用handlebar.js登录状态相关菜单
我尝试应用以下逻辑:Javascript 使用handlebar.js登录状态相关菜单,javascript,html,parse-platform,handlebars.js,Javascript,Html,Parse Platform,Handlebars.js,我尝试应用以下逻辑: var LoggedOutMenuView = Parse.View.extend({ template: Handlebars.compile($('#menu-logged-out-tpl').html()), render: function(){ this.$el.html(this.template()); } }); var LoggedInMenuView = Parse.View.extend({ templ
var LoggedOutMenuView = Parse.View.extend({
template: Handlebars.compile($('#menu-logged-out-tpl').html()),
render: function(){
this.$el.html(this.template());
}
});
var LoggedInMenuView = Parse.View.extend({
template: Handlebars.compile($('#menu-logged-in-tpl').html()),
render: function(){
this.$el.html(this.template());
}
});
var currentUser = Parse.User.current();
if (currentUser) {
var loggedInMenuView = new LoggedInMenuView();
loggedInMenuView.render();
$('.navbar-fixed').html(loggedInMenuView.el);
} else {
var loggedOutMenuView = new LoggedOutMenuView();
loggedOutMenuView.render();
$('.navbar-fixed').html(loggedOutMenuView.el);
}
…致:
BlogApp.Views.Categories = Parse.View.extend({
className: 'sidebar-module',
template: Handlebars.compile($('#menu-logged-out-tpl').html()),
render: function() {
var collection = { category: this.collection.toJSON() };
this.$el.html(this.template(collection));
}
});
目前,在上面的第二段代码中,我目前只呈现#menu logged out tpl,但我想让它以用户是否登录为条件,如前所述
为什么我不能执行以下操作,我应该做什么?
BlogApp.Views.Categories = Parse.View.extend({
className: 'sidebar-module',
var currentUser = Parse.User.current();
if (currentUser) {
template: Handlebars.compile($('#menu-logged-in-tpl').html()),
} else {
template: Handlebars.compile($('#menu-logged-out-tpl').html()),
}
render: function() {
var collection = { category: this.collection.toJSON() };
this.$el.html(this.template(collection));
}
});
因为在对象内部使用
if
,所以不能这样做。使用a指定模板值
BlogApp.Views.Categories = Parse.View.extend({
className: 'sidebar-module',
template: Handlebars.compile(Parse.User.current() ? $('#menu-logged-in-tpl').html() : $('#menu-logged-out-tpl').html()),
render: function() {
var collection = {
category: this.collection.toJSON()
};
this.$el.html(this.template(collection));
}
});