问题
I'm working on a shared library that has internal Ember Data Models that are used by the calling application. However, those DS.Model objects from the library don't find their way into the store for the calling application.
library.js
DS.SharedLibrary.User = DS.Model.extend({});
DS.SharedLibrary.BaseModel = DS.Model.extend({
createdBy: DS.belongsTo('DS.SharedLibrary.User')
});
app.js
App.MyModel = DS.SharedLibrary.BaseModel.extend({
customField: DS.attr()
});
However, when the adapter goes to resolve the createdBy relationship:
Error: No model was found for 'DS.SharedLibrary.BaseModel'
How can I inform my store of objects that exist in another namespace?
回答1:
You can register your custom models by setting up an Application.initializer inside the library:
Ember.Application.initializer({
name: 'myLibrary-models',
initialize: function(container, application) {
//one of these calls for each model you need to register
application.register('model:DS.SharedLibrary.User', DS.SharedLibrary.User);
}
});
Then your application's models can reference them like any other:
App.MyModel = DS.Model.extend({ createdBy: DS.belongsTo('DS.SharedLibrary.User') });
来源:https://stackoverflow.com/questions/23109418/registering-models-from-another-namespace-with-the-ember-data-store