1

I want to allow something like the following to work in my application:

store.find('my-addon.my-addon-model', 1)

store.find('my-addon/my-addon-model', 1)

store.find('my-addon:my-addon-model', 1) (unlikely)

The thing is I want it to search for a model that is 100% defined in an addon.

import MyAddonModel from 'my-addon/models/my-addon-model' works from within my app - but container resolution doesn't...

How would I do/allow this?

jmurphyau
  • 2,309
  • 13
  • 11

1 Answers1

1

This question is the same as:

Registering models from another namespace with the Ember Data store

However the naming there is a bit confusing. When trying this out I also seem to have hit a bug in ember-data@1.0.0-beta.15.

What you need to do in your module initializer is register the model(s) to your application.

import Ember from 'ember';    
import MyModel from my-module/models/my-model';    
export default {
    name: 'my-module-models',

    initialize: function(container, application) {
        //one of these calls for each model you need to register
        application.register('model:myModule.myModel',MyModel);
    }
};

Now according to my experience with Ember and Ember-Data, this is supposed to work just like that. But in my setup with ember-data@1.0.0-beta.15 there seems to be an issue determining the models "key" after creation. The model instance is created fine, but you will hit an error trying to save the model.

I've found a quick workaround, but it's a hack and I would need to investigate further whether I'm missing a step or it's a bug. The workaround involves setting the "typeKey" of the class, resulting in:

import MyModel from my-module/models/my-model';    
export default {
    name: 'my-module-models',

    initialize: function(container, application) {
        //one of these calls for each model you need to register
        application.register('model:myModule.myModel',MyModelreopenClass({typeKey:'myModule.myModel'}));
    }
};

Finally, there is another way around. When creating modules in ember-cli, you have the app folder which will be merged with the app using your module. You could place default extends of your model(s) in the app folder.

Community
  • 1
  • 1
emrvb
  • 21
  • 3
  • Thanks for the response. An initialiser was the way I was heading towards because I don't know any other way but I was hoping for another way.. A way that still allowed for lazy loading and doesn't require the work around you're talking about.. But the workaround is something I didn't plan for so if I do go with an initializer that helps a lot thank you. – jmurphyau Mar 18 '15 at 00:45