我有一个使用 JSONAPI 适配器的 ember-cli 项目,通过adapters/application.js
. 我想添加一个应该使用ember-localstorage-adapter
. 我声明了模型以及相应的适配器和序列化器:
models/widget.js
import DS from 'ember-data';
export default DS.Model.extend({...});
adapters/widget.js
import LSAdapter from 'ember-localstorage-adapter';
export default LSAdapter.extend({});
serializers/widget.js
import LSSerializer from 'ember-localstorage-adapter';
export default LSSerializer.extend({});
并在我的路线中加载这个模型:
routes/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('widget');
}
});
这应该可以,但我得到一个缺少函数类型的错误:
ember.debug.js:19755 TypeError: serializer.normalizeResponse is not a function
at normalizeResponseHelper (serializer-response.js:80)
at finders.js:147
at Object.run (ember.debug.js:295)
at Class._adapterRun (store.js:2056)
at finders.js:146
at tryCatch (ember.debug.js:52052)
at invokeCallback (ember.debug.js:52067)
at publish (ember.debug.js:52035)
at ember.debug.js:41262
at invoke (ember.debug.js:991)
好的,我添加缺少的方法:
serializers/widget.js
import LSSerializer from 'ember-localstorage-adapter';
export default LSSerializer.extend({
normalizeResponse: function(store, modelClass, payload, id, requestType) {
return payload;
}
});
现在错误与有效载荷有关:
ember.debug.js:19755 Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
* One or more of the following keys must be present: "data", "errors", "meta".
at new Error (native)
at Error.EmberError (../assets/vendor.js:30247:21)
at assert (../assets/vendor.js:17276:13)
at Object.assert (../assets/vendor.js:30059:34)
at assert (../assets/vendor.js:97498:37)
at normalizeResponseHelper (../assets/vendor.js:107520:39)
为什么这样断言?我不能将 JSON API 适配器与不合格的适配器混合并匹配吗?我知道如果模型尝试进行交互(例如,拥有一个/属于等关系)可能会出现问题,但没有这样的事情。我只是想添加一个不受我的后端(符合 JSON-API 的)REST api 支持的新模型。
Ember:2.7.2
嵌入数据:2.7.0