使用Ember Data存储从另一个命名空间注册模型 [英] Registering models from another namespace with the Ember Data store
问题描述
DS.Model
对象不能找到他们进入调用应用程序的存储
的方式。 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()
});
但是,当适配器解决 createdBy
关系:
错误:没有找到DS.SharedLibrary.BaseModel的模型
如何通知我存储在另一个命名空间中的对象?
您可以通过在库中设置 Application.initializer
来注册您的自定义模型:
Ember.Application.initializer({
name:'myLibrary-models',
initialize:函数(容器,应用程序){
//每个模型的这些调用之一,您需要注册
application.register('model:DS.SharedLibrary.User',DS.SharedLibrary.User);
}
});
然后,您的应用程序的模型可以像其他任何参考一样:
App.MyModel = DS.Model.extend({createdBy:DS.belongsTo('DS.SharedLibrary.User')});
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?
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') });
这篇关于使用Ember Data存储从另一个命名空间注册模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!