Skip to content

Instantly share code, notes, and snippets.

@grayt0r
Last active October 30, 2020 13:30
Show Gist options
  • Save grayt0r/d5d0c5b4b79815305ad427fdf7ff04f2 to your computer and use it in GitHub Desktop.
Save grayt0r/d5d0c5b4b79815305ad427fdf7ff04f2 to your computer and use it in GitHub Desktop.
pushPayload issue
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default class extends Model {
@attr name;
}
import Route from '@ember/routing/route';
const fooJson = {
foo: {
id: 1,
name: "One"
},
_foos: [{
id: 2,
name: "Two"
}]
};
const server = new Pretender();
server.map(function() {
this.get('/foos/1', function() {
return [200, {}, JSON.stringify(fooJson)];
});
});
export default class ApplicationRoute extends Route {
async model() {
// Swap these lines to see the difference in behaviour
this.store.pushPayload('foo', fooJson);
// this.store.findRecord('foo', 1);
return this.store.peekAll('foo');
}
}
import RESTSerializer from '@ember-data/serializer/rest';
export default class ApplicationSerializer extends RESTSerializer {
normalize() {
console.log('***** normalize');
return super.normalize(...arguments);
}
normalizeResponse() {
console.log('***** normalizeResponse');
return super.normalizeResponse(...arguments);
}
modelNameFromPayloadKey(payloadKey) {
console.log('***** modelNameFromPayloadKey', payloadKey);
if (payloadKey.charAt(0) === '_') {
// Uncommenting this line fixes the problem
// payloadKey = payloadKey.substr(1);
}
return super.modelNameFromPayloadKey(payloadKey);
}
}
<h1>Foos</h1>
<ul>
{{#each this.model as |foo|}}
<li>{{foo.name}}</li>
{{/each}}
</ul>
{
"version": "0.17.1",
"EmberENV": {
"LOG_VERSION": false,
"EXTEND_PROTOTYPES": false,
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1",
"pretender": "https://unpkg.com/[email protected]/dist/pretender.bundle.js"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "3.18.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment