I have an ember-cli project which uses JSONAPI adapter, via adapters/application.js
. I want to add a model class that should use the ember-localstorage-adapter
. I declare the model and the respective adapter and serializer:
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({});
and load this models in my route:
routes/index.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('widget');
}
});
This should work, but I get a missing function type error:
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)
OK, I add the missing method:
serializers/widget.js
import LSSerializer from 'ember-localstorage-adapter';
export default LSSerializer.extend({
normalizeResponse: function(store, modelClass, payload, id, requestType) {
return payload;
}
});
And now the error is about the 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)
Why is this asserted? Can't I mix and match a JSON API adapter with a non-conforming one? I understand there could be problems if the models would try to intreact (eg. has-one/belongs-to etc relationships) but there is no such thing. I'm just trying to add a new model that is not backed by my back-end (JSON-API conforming) REST api.
Ember: 2.7.2
Embed Data: 2.7.0
First your import is wrong!
Its not
import LSSerializer from 'ember-localstorage-adapter';
but
import {LSSerializer} from 'ember-localstorage-adapter';
This is because of the exports in ember-localstorage-adapter
:
import LSAdapter from 'ember-localstorage-adapter/adapters/ls-adapter';
import LSSerializer from 'ember-localstorage-adapter/serializers/ls-serializer';
export {
LSAdapter,
LSSerializer
};
export default LSAdapter;
With import LSSerializer from 'ember-localstorage-adapter';
you import the default
export wich is not the serializer.
Next you need to understand that ember-data
internally works with JSONAPI
documents. Its the function of the serializer to serialize whatever the adapter is fetching into JSONAPI
. This is defined by the store
and ember-data
itself. See the API specification.
Not lets see what this code from you is doing:
serializers/widget.js
import LSSerializer from 'ember-localstorage-adapter';
export default LSSerializer.extend({
normalizeResponse: function(store, modelClass, payload, id, requestType) {
return payload;
}
});
You are actually returning an adapter where you implement the normalizeResponse
function. That makes it an valid serializer, that is doing nothing! You directly return what is responded by your adapter, which seems to be not valid JSONAPI. Thats why this error occurs.