如何获取应用程序的上下文(路由名称)以用于样式目的?

时间:2014-01-26 12:14:25

标签: ember.js

路线非常简单:

App.Router.map(function() {
  this.resource('users', {path: '/'}, function() {
    this.resource('user', {path: '/user/:user_id'});
  });
});

以及以下模板:

# application
.container= outlet

# users
.left
  ul.users-list= each controller
    li= link-to 'user' this
      = fullName
.main
  = outlet

# users/index
p please select user from the list

是否可以获得当前的应用状态?例如,知道usersIndexRoute处于活动状态(空白状态)以不同地设置.main样式是有用的。 我希望在应用程序视图中具有此状态,例如在.container中绑定。

2 个答案:

答案 0 :(得分:4)

重复@Panagiotis Panagi's回答,您只需将classForPath属性绑定到应用程序视图,如下所示:

App.ApplicationView = Ember.View.extend({
  classNames: ['app'], // Static classes here.
  classNameBindings: ['classForPath'], // Your dynamic path here.
  classForPath: function() {
    var currentPath = this.get('controller.currentPath') || '';
    currentPath = Ember.String.decamelize(currentPath);

    // Note we prefix with a string 'current-path-' in case the route is 
    // an integer e.g., 404. CSS classes cannot start with numbers.
    currentPath = 'current-path-' + currentPath.split('.').join('-');
    return currentPath;
  }.property('controller.currentPath')
});

答案 1 :(得分:2)

使用currentPath的{​​{1}}获取当前路径路径并将其绑定到ApplicationController类:

application_view.js

container

application.handlebars

App.ApplicationView = Ember.View.extend({
  classForPath: (function() {
    var currentPath = this.get('controller.currentPath') || '';
    currentPath = Ember.String.decamelize(currentPath);
    currentPath = currentPath.split('.').join('-');
    return currentPath;
  }).property('controller.currentPath')
});