如何获取所有ember-data模型类的列表? [英] How to get a list of all ember-data Model classes?

查看:144
本文介绍了如何获取所有ember-data模型类的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 store.findRecord(modelTypeName,id); ;如何获取我的应用程序中可用的所有可能的 modelTypeName 的列表?



我正在尝试我在使用Ember 2.1.0的ember-cli版本1.13.8



编辑此问题有点重复,但在他正在工作的上下文中并不清楚。在一般情况下,您应该访问应用程序(实例)的一个(实例)初始化程序中,似乎应该更容易实现内省,因此希望这个问题可以积累一些特定于该上下文的有用答案。

解决方案

tl; dr:



< >从你的代码(例如一个实例初始化程序):

 从'ember-resolver / utils / module-registry ; 
...
var modelRegexp = /^[a-zA-Z0-9-_]+\/models\/(.*)$/;
var modelNames = new ModuleRegistry()
.moduleNames()
.filter((name)=> modelRegexp.test(name))
.map((name)= > modelRegexp.exec(name)[1]);

如果您在正在运行的应用程序中需要调试控制台上的一个黑客单行:

  YourAppName .__ container__ 
.lookup('container-debug-adapter:main')
.catalogEntriesByType('model' )
.filter((name)=> YourAppName .__ container __。lookupFactory('model:'+ name));

或者只获取已经加载的模型:



($)$($)$ {code} Object.keys(YourAppName .__ container __。factoryCache).filter((i)=> i.startsWith('model:'))

更长的回答:



复杂的,假设您不使用pod或其他任何非标准的,并且依赖于未记录的内部API。它们还专门针对 ember-resolver (基于ES6模块的查找系统用于ember-cli和Ember App Kit)。所以不太理想。



它应该是一样简单:

  var debugAdapter = appInstance.lookup('container-debug-adapter:main'); 
var modelNames = debugAdapter.catalogEntriesByType('model');

不幸的是,包含在ember-resolver中的ContainerDebugAdapter类是错误的,所以它将返回一堆额外的项目。



希望这将很快修复(我已经提交了不正确) 。 ContainerDebugAdapter与Ember 一起运行,在应用程序范围的命名空间( Ember.Namespace.NAMESPACES )中查找对象也是一样的。



我意识到,Ember(和ember-cli特别)是非常名称约定的驱动,但这个正则表达式匹配感觉有点疯狂。在我看来,理想情况下,我们将导入DS.Model类,只是获取它的子类。不幸的是,似乎不是(容易)可能的:据我所知,Ember类存储对它的超类的引用,但不是它的子类。


Individual models can be fetched with store.findRecord(modelTypeName, id);; how can I get a list of all the possible modelTypeNames available in my app?

I'm specifically trying to do this from within an app initializer.

I'm using ember-cli version 1.13.8 with Ember 2.1.0

Edit: This question is somewhat of a duplicate, but it's not clear in what context he's working. It seems like introspection should be easier to achieve without hacks in an (instance) initializer where you're supposed to access to the app (instance) than in the general case, so hopefully this question can accumulate some useful answers specific to that context.

解决方案

tl;dr:

From within your code (eg. an instance intializer):

import ModuleRegistry from 'ember-resolver/utils/module-registry';
    ...
var modelRegexp = /^[a-zA-Z0-9-_]+\/models\/(.*)$/;
var modelNames = new ModuleRegistry()
  .moduleNames()
  .filter((name) => modelRegexp.test(name))
  .map((name) => modelRegexp.exec(name)[1]);

If you need a hacky one-liner from the debugging console on a running app:

YourAppName.__container__
  .lookup('container-debug-adapter:main')
  .catalogEntriesByType('model')
  .filter((name) => YourAppName.__container__.lookupFactory('model:' + name));

Or, get only the models already loaded with:

Object.keys(YourAppName.__container__.factoryCache).filter((i) => i.startsWith('model:'))

Longer answer:

These approaches are kind of complicated, assume you aren't using pods or anything else non-standard, and rely on undocumented internal APIs. They're also specific to ember-resolver (the ES6-module-based lookup system used in ember-cli and Ember App Kit). So not ideal.

It should actually be as simple as:

var debugAdapter = appInstance.lookup('container-debug-adapter:main');
var modelNames = debugAdapter.catalogEntriesByType('model');

Unfortunately the ContainerDebugAdapter class included with ember-resolver is buggy, so it will return a bunch of extra items.

Hopefully this will be fixed soon (I've filed bug report), but until then, the code above should serve.

Interestingly, this string-based matching is basically what the ContainerDebugAdapter does (incorrectly) internally. The ContainerDebugAdapter that ships with Ember does the same sort of thing to look up objects in the app-wide namespaces (Ember.Namespace.NAMESPACES).

I realize that Ember (and ember-cli especially) is very name-convention-driven, but this regex matching feels a little crazy. It seems to me that, ideally, we'd instead be importing the DS.Model class and just getting it's subclasses. Unfortunately it seems that isn't (easily) possible: as far as I can tell, an Ember class stores a reference to it's superclass(es), but not to it's subclasses.

这篇关于如何获取所有ember-data模型类的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆