ember.js - Why is ember-data 2.7.0 normalizeHelper expecting a JSON API response from my custom adapter? -
i have ember-cli project uses jsonapi adapter, via adapters/application.js
. want add model class should use ember-localstorage-adapter
. declare model , respective adapter , serializer:
models/widget.js import ds 'ember-data'; export default ds.model.extend({...}); adapters/widget.js import lsadapter 'ember-localstorage-adapter'; export default lsadapter.extend({}); serializers/widget.js import lsserializer 'ember-localstorage-adapter'; export default lsserializer.extend({});
and load models in route:
routes/index.js import ember 'ember'; export default ember.route.extend({ model() { return this.store.findall('widget'); } });
this should work, missing function type error:
ember.debug.js:19755 typeerror: serializer.normalizeresponse not function @ normalizeresponsehelper (serializer-response.js:80) @ finders.js:147 @ object.run (ember.debug.js:295) @ class._adapterrun (store.js:2056) @ finders.js:146 @ trycatch (ember.debug.js:52052) @ invokecallback (ember.debug.js:52067) @ publish (ember.debug.js:52035) @ ember.debug.js:41262 @ invoke (ember.debug.js:991)
ok, add missing method:
serializers/widget.js import lsserializer 'ember-localstorage-adapter'; export default lsserializer.extend({ normalizeresponse: function(store, modelclass, payload, id, requesttype) { return payload; } });
and error payload:
ember.debug.js:19755 error: assertion failed: normalizeresponse must return valid json api document: * 1 or more of following keys must present: "data", "errors", "meta". @ new error (native) @ error.embererror (../assets/vendor.js:30247:21) @ assert (../assets/vendor.js:17276:13) @ object.assert (../assets/vendor.js:30059:34) @ assert (../assets/vendor.js:97498:37) @ normalizeresponsehelper (../assets/vendor.js:107520:39)
why asserted? can't mix , match json api adapter non-conforming one? understand there problems if models try intreact (eg. has-one/belongs-to etc relationships) there no such thing. i'm trying add new model not backed back-end (json-api conforming) rest api.
ember: 2.7.2
embed data: 2.7.0
first import wrong!
its not
import lsserializer 'ember-localstorage-adapter';
but
import {lsserializer} 'ember-localstorage-adapter';
this because of exports in ember-localstorage-adapter
:
import lsadapter 'ember-localstorage-adapter/adapters/ls-adapter'; import lsserializer 'ember-localstorage-adapter/serializers/ls-serializer'; export { lsadapter, lsserializer }; export default lsadapter;
with import lsserializer 'ember-localstorage-adapter';
import default
export wich not serializer.
next need understand ember-data
internally works jsonapi
documents. function of serializer serialize whatever adapter fetching jsonapi
. defined store
, ember-data
itself. see api specification.
not lets see code doing:
serializers/widget.js import lsserializer 'ember-localstorage-adapter'; export default lsserializer.extend({ normalizeresponse: function(store, modelclass, payload, id, requesttype) { return payload; } });
you returning adapter implement normalizeresponse
function. makes valid serializer, doing nothing! directly return responded adapter, seems not valid jsonapi. thats why error occurs.
Comments
Post a Comment