2

I'm trying to use ember-data, and I need to be able to dynamically resolve a model name given a String.

I looked into the ember-data codebase, but couldn't find anything there. Then I found this in ember:

/**
  @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) {

I would have assumed that ember-data hooked into this functionality so I could do sth like this:

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

But alas, no...

Perhaps this is the way to go? (from store.js line ~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: []
      });
    }
  },

Looks like the store has some kind of typeMaps internally?

1 Answers1

0

The serializer knows how to do this if you declared your model. See DS.Serializer typeFromAlias

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

Where does you string come from?

Cyril Fluck
  • 1,561
  • 7
  • 9