2016-02-02 4 views
2

Я пытаюсь применить следующую логику:Войти состояние зависимого меню с помощью 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-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)); 
    } 

}); 

ответ

2

Поскольку вы используете if внутри объекта, вы не можете сделать это. Используйте значение conditional operator, чтобы назначить значение шаблона.

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)); 
    } 
}); 
Смежные вопросы