【问题标题】:Set Ember Application View Class for Homepage Only仅为主页设置 Ember 应用程序视图类
【发布时间】:2014-03-10 22:59:09
【问题描述】:

免责声明:我对 Ember 还很陌生,所以请放轻松。 :)

如果 App.get('currentPath') 的值设置为'index',我正在尝试在应用程序视图的 div 上显示一个“is-home”的类。下面的代码用于设置类,但是,它在加载时这样做。如果您加载页面并单击链接,则如果您离开主页,则不会重新运行逻辑以删除该类,或者如果您在其他页面上输入但随后导航到主页,则不会添加它。我确信我错过了一些简单的东西。

任何帮助将不胜感激!

当前代码(注意:我使用的是 Ember App Kit)

// app/views/application.js

export default Ember.View.extend({
  classNameBindings: ['isHome'],
  isHome: function() {
    if (App.get('currentPath') == 'index') {
      return true;
    } else {
    return false;
    }
  }.property()
});

App.get('currentPath') 的功能来源于这个答案https://stackoverflow.com/a/18302740/3403881

【问题讨论】:

    标签: ember.js ember.js-view


    【解决方案1】:

    currentPath 属性发生变化时,您必须告诉该属性进行更新。

    isHome: function() { ... }.property('currentPath')
    

    但是,我不能 100% 确定它是否会在视图上找到 currentPath 属性,因为它在技术上存在于控制器上。你可能不得不观看controller.currentPath

    【讨论】:

    • 您在视图中没有找到该属性是正确的。然而,.property('controller.currentPath') 成功了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 2016-11-21
    • 2012-06-19
    相关资源
    最近更新 更多