Aar*_*rck 5 javascript ember.js
由于Ember App Kit使用ES6模块,我对放置或配置基类的代码的最佳位置感到困惑.例如,以下代码允许对无散列URL使用html5历史记录:
//enable HTML5
Router.reopen({ location: 'history' });
Run Code Online (Sandbox Code Playgroud)
或者以下代码为所有视图提供了一个afterRender挂钩:
Ember.View.reopen({
didInsertElement : function() {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.didRenderElement);
}
});
Run Code Online (Sandbox Code Playgroud)
根据我在阅读Ember App Kit的github上的问题以及stackoverflow上的各种答案后的理解,它非常适合ES6模块仅导出而不包含任何"副作用",换句话说,代码不属于导出的对象.然而,似乎副作用技术目前正在使用,甚至被许多开发人员建议.例如,我将html5历史配置放在router.js模块中:
/**
* Ember Router
*
* @module Router
* @exports appkit/Router
*/
var Router = Ember.Router.extend({});
//enable HTML5
Router.reopen({ location: 'history' });
Router.map(function() {
this.route('login');
this.route('logout');
this.route('dashboard');
this.route('orders');
this.route('customers');
});
export default Router;
Run Code Online (Sandbox Code Playgroud)
您可以看到模块导出了Router对象,但是reopen语句只是松散地放在文件中.
为了保持组织有序并利用这项技术,我想我可以创建一个配置文件夹,然后在文件夹中创建模块,如view.js,router.js等:
view.js
/**
* Ember ViewConfig
*
* @module ViewConfig
* @exports appkit/ViewConfig
*/
Ember.View.reopen({
didInsertElement : function() {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.didRenderElement);
}
});
export default {};
Run Code Online (Sandbox Code Playgroud)
app.js
/*
* Config Classes
*/
import ViewConfig from 'appkit/config/view';
import RouterConfig from 'appkit/config/router';
...
Run Code Online (Sandbox Code Playgroud)
这有效,但我觉得我打破了一个惯例.这有意义还是我错过了什么?
编辑: 我想另一种解决方案是使用函数导出对象,例如applyConfig,然后在App.js中导入该函数后调用.但这并没有解决我仍然在App.js中使用"副作用"的事实.
由于这个问题最近得到了支持,我想提供一个从 Ember 应用程序套件的作者之一 (stefanpenner) 那里收到的答案。您可以在这里阅读讨论:https ://github.com/stefanpenner/ember-app-kit/issues/485
基本上我走在正确的道路上,而不是config文件夹,他建议使用ext文件夹并导入到app.js. 我想“副作用”并不是什么大问题。但他确实承认“这确实让人感觉有点老套”,并且他愿意接受建议......
| 归档时间: |
|
| 查看次数: |
324 次 |
| 最近记录: |