如何在Ember CLI中使用ES6语法对Ember类进行子类化

时间:2015-01-11 02:29:57

标签: javascript ember.js

我想在Ember CLI中创建自定义Route类。我有以下示例在使用全局变量编写的旧应用程序中工作:

App.AuthenticatedRoute = Ember.Route.Extend({ 
  beforeModel: function() { 
    //Do some things
  }
});

App.DashboardRoute = App.AuthenticatedRoute.Extend({});

我对ES6模块非常熟悉,知道这个例子看起来像这样......

var AuthenticatedRoute = Ember.Route.Extend({
  beforeModel: function() { 
    //
  }
});

export default AuthenticatedRoute;

......但我对以下内容感到好奇:

  1. 应用程序结构中的哪个位置?
  2. 如何在其他模块中访问此子类?
  3. 更新

    澄清我的问题:我正在寻找有关这样的自定义实现应该在哪里生活的信息,而不是生活在app / routes目录中的常规子路由。 Ember CLI文档提出以下内容:

    To provide a custom implementation for generated routes (equivalent to App.Route when using globals), use app/routes/basic.js.

    http://www.ember-cli.com/#module-directory-naming-structure

    ...但我在实践中找不到任何这方面的例子,这似乎是一个不完整的惯例。我最终将自定义实现视为标准路由(app / routes / authenticated.js)并根据需要导入。

1 个答案:

答案 0 :(得分:0)

如果您使用强烈推荐的ember-cli,它将存在于app/routes/authenticated.js中,如下所示:

import Ember from 'ember';

export default Ember.Route.extend({
});

然后您可以将其作为

导入其他模块

import authRoute from 'app/routes/authenticated'