Ember.js - 获取所有型号名称 [英] Ember.js - Get all model names
问题描述
我正在尝试在我的Ember应用程序(v1.8)中获取所有定义模型的列表。
I'm trying to to get a list of all defined models in my Ember application (v1.8).
从我的研究可以看出,Container.resolver有一些神秘的魔法。容器是否有所有活动模型的列表,还是有任何方式来获取它们?
As far as I can tell from my research, the Container.resolver works with some mysterious magic. Does the Container have a list of all active models, or is there any way to get them?
感谢:)
@edit
kingpin2k的回答有效,但不是在Ember-Cli,有没有其他的方法?
kingpin2k's answer works, but not in Ember-Cli, is there an other way?
推荐答案
所以,对于Ember Cli来说,我没有找到直接的解决方案。然而,您可以通过一些创意编码解决问题,并通过分析ember-inspector的来源。
So, for Ember Cli there isn't a direct solution I could find. However, you can work around the problem with a bit of creative coding and by analysing ember-inspector's source.
var models = Object.keys(require._eak_seen).filter(function(module) {
return module.indexOf(config.modulePrefix + '/models/') === 0;
});
此代码段迭代了使用requirejs注册的模块,仅提取所需的模块(我们的模型)
This snippet iterates over the modules registered with requirejs and extracts only the wanted modules (the models in our case).
对于config.modulePrefix部分,您将需要导入您的conf文件(修复路径):
For the "config.modulePrefix" part to work you will need to import your conf file (fix the path):
import config from '../config/environment';
或者,您可以将config.modulePrefix硬编码为myappname或使用: p>
Alternatively you can either hardcode the "config.modulePrefix" to "myappname" or use this:
this.container.resolver.__resolver__.namespace.modulePrefix
PS:检查您需要使用的型号:
PS: To inspect the model you need to use:
require(_the_model_module_name).default
这篇关于Ember.js - 获取所有型号名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!