Javascript 我应该把课程重开放在余烬应用程序包的哪里?
由于Ember应用程序工具包使用ES6模块,所以我对放置扩展或配置基类的代码的最佳位置有点困惑。例如,以下代码支持对无哈希URL使用html5历史记录:Javascript 我应该把课程重开放在余烬应用程序包的哪里?,javascript,ember.js,Javascript,Ember.js,由于Ember应用程序工具包使用ES6模块,所以我对放置扩展或配置基类的代码的最佳位置有点困惑。例如,以下代码支持对无哈希URL使用html5历史记录: //enable HTML5 Router.reopen({ location: 'history' }); 或以下为所有视图提供afterRender挂钩的代码: Ember.View.reopen({ didInsertElement : function() { this._super(); Em
//enable HTML5
Router.reopen({ location: 'history' });
或以下为所有视图提供afterRender挂钩的代码:
Ember.View.reopen({
didInsertElement : function() {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.didRenderElement);
}
});
阅读了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;
您可以看到模块导出了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 {};
/*
* Config Classes
*/
import ViewConfig from 'appkit/config/view';
import RouterConfig from 'appkit/config/router';
...
app.js
/**
* Ember ViewConfig
*
* @module ViewConfig
* @exports appkit/ViewConfig
*/
Ember.View.reopen({
didInsertElement : function() {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.didRenderElement);
}
});
export default {};
/*
* Config Classes
*/
import ViewConfig from 'appkit/config/view';
import RouterConfig from 'appkit/config/router';
...
这是可行的,但我觉得我打破了惯例。这有意义吗?还是我遗漏了什么
编辑:
我认为另一种解决方案是使用函数导出对象,例如applyConfig,然后在App.js中导入后调用该函数。但这并不能解决我仍然会在App.js中使用“副作用”这一事实。由于这一点最近得到了改进,我想提供一个答案,我从余烬应用程序工具包的一位作者(stefanpenner)那里得到了答案。您可以在此处阅读讨论: 基本上我走的是正确的道路,他建议使用
ext
文件夹,而不是config
文件夹,然后导入app.js
。我想“副作用”没什么大不了的。虽然他承认“这确实让人觉得有些骇人”,但他还是愿意接受建议