使用车把的登录状态相关菜单.js
Login state dependent menu using 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({
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 登录的 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
,所以无法执行此操作。使用条件运算符分配模板值。
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));
}
});
相关文章:
- 将js添加到wordpress中以突出显示css活动菜单
- 尝试创建一个具有z-index的堆叠菜单'使用HTML5/CSS3/JS
- JS在选择下拉菜单打开时创建延迟
- JS下拉菜单没有'不起作用
- JS下拉菜单显示在图像旋转器上
- 使用 d3.js 添加下拉菜单
- jQuery/js- 如何从基于 href 的类中获取菜单名称
- 无法在 NW.js 中切换窗口菜单栏可见性
- Bootstrap Collapse.js切换菜单的隐藏/显示
- Angular Js弹出菜单
- 如何解决使用easyResponsiveTabs.js时固定菜单的闪烁问题
- 将上下文菜单(context.js)附加到cytoscape.js中的节点
- HTML、CSS、JS响应下拉菜单
- 主菜单JS在IE和Firefox中不起作用(其他菜单起作用)
- 响应菜单 js (clear dom?)
- 使用车把的登录状态相关菜单.js
- CSS 菜单 - 单击时的子菜单 (JS)
- 多个搜索选项,单个字段,通过下拉菜单js/jquery进行选择
- 移动菜单(js)没有加载在WordPress WPML网站上
- 在 Ajax 之后重新绑定菜单 JS