Registering models from another namespace with the Ember Data store

余生颓废 提交于 2019-12-13 04:40:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!