簡體   English   中英

如何從Ember數據中的字符串名稱解析模型類?

[英]How do I resolve a model class from a String name in Ember data?

我正在嘗試使用ember-data,並且我需要能夠動態解析給定String的模型名稱。

我查看了余燼數據庫代碼庫,但在那里找不到任何內容。 然后我在余燼中發現了這個:

/**
  @private

  This function defines the default lookup rules for container lookups:

  * templates are looked up on `Ember.TEMPLATES`
  * other names are looked up on the application after classifying the name.
    For example, `controller:post` looks up `App.PostController` by default.
  * if the default lookup fails, look for registered classes on the container

  This allows the application to register default injections in the container
  that could be overridden by the normal naming convention.

  @param {Ember.Namespace} namespace the namespace to look for classes
  @return {any} the resolved value for a given lookup
*/
function resolverFor(namespace) {

  resolve: function(fullName) {
    return this.resolver(fullName) || this.registry.get(fullName);
  },

  normalize: function(fullName) {
    return fullName;
  },

  lookup: function(fullName, options) {

我本以為可以將ember-data掛接到此功能中,所以可以這樣做:

App.resolver.resolveModel "model:#{modelName}"

但是a,不...

也許這是要走的路? (來自store.js行〜1500)

  typeMapFor: function(type) {
    var typeMaps = get(this, 'typeMaps');
    var guidForType = Ember.guidFor(type);

    var typeMap = typeMaps[guidForType];

    if (typeMap) {
      return typeMap;
    } else {
      return (typeMaps[guidForType] =
        {
          idToCid: {},
          clientIds: [],
          recordArrays: []
      });
    }
  },

看起來商店內部有某種typeMap?

如果聲明了模型,則序列化程序將知道如何執行此操作。 請參閱DS.Serializer typeFromAlias

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/serializer.js#L1103

你的繩子從哪里來?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM