Skip to content

Instantly share code, notes, and snippets.

@sdhull
Created September 6, 2018 06:02
Show Gist options
  • Save sdhull/ea8b22e6fef7572ed3e233952c309e44 to your computer and use it in GitHub Desktop.
Save sdhull/ea8b22e6fef7572ed3e233952c309e44 to your computer and use it in GitHub Desktop.
push vs pushPayload
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
namespace: '/api'
});
import Ember from 'ember';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';
import { cloneDeep } from 'lodash';
export default Ember.Controller.extend({
appName: 'push vs pushPayload',
store: service(),
rawAuthor: computed('model.rawAuthor', function() {
return this.get('model.rawAuthor');
}),
authorSerialized: computed(function() {
return this.get('model.author').serialize({includeId: true});
}).volatile(),
authorNormalized: computed('model.author', function() {
let json = cloneDeep(this.get('authorSerialized'))
let ModelClass = this.store.modelFor('author');
let serializer = this.store.serializerFor('author');
let normalized = serializer.normalizeResponse(this.store, ModelClass, json, null, 'query');
return normalized;
}),
actions: {
testPush() {
console.log('test push');
let json = this.get('model.author').serialize({includeId: true});
console.log(json);
// try {
this.store.push(json);
// } catch(e) {
// alert(e.message);
// throw e;
// }
},
testPushPayload() {
console.log('test pushPayload');
let json = this.get('model.author').serialize({includeId: true});
console.log(json);
// try {
this.store.pushPayload('authors', json);
// } catch(e) {
// alert(e.message);
// throw e;
// }
},
testPushNormalized() {
console.log('test push normalized');
// try {
this.store.push(this.get('authorNormalized'));
// } catch(e) {
// alert(e.message);
// throw e;
// }
},
testPushPayloadNormalized() {
console.log('test pushPayload normalized');
// try {
this.store.pushPayload('author', this.get('authorNormalized'));
// } catch(e) {
// alert(e.message);
// throw e;
// }
},
}
});
import Ember from 'ember';
export function prettyPrint([json]/*, hash*/) {
return JSON.stringify(json, null, 2);
}
export default Ember.Helper.helper(prettyPrint);
import Mirage from 'ember-cli-mirage';
export default function() {
window.server = this;
this.namespace = '/api';
this.get('/authors/:id', (schema, request) => {
console.log('getting author id 1');
return {
"data": {
"type": "authors",
"id": "1",
"attributes": {
"name": "George Orwellk"
}
}
}
})
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
name: attr('string'),
});
import Ember from 'ember';
import { hash } from 'rsvp';
export default Ember.Route.extend({
model() {
const author = this.store.findRecord('author', 1);
const rawAuthor = $.get('/api/authors/1');
return hash({author, rawAuthor});
},
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<br>
<br>
<br>
<a href {{action 'testPush'}}>test store.push()</a>
<br>
<a href {{action 'testPushPayload'}}>test store.pushPayload() pluralized model name</a>
<br>
<a href {{action 'testPushNormalized'}}>test store.push() with normalized json</a>
<br>
<a href {{action 'testPushPayloadNormalized'}}>test store.pushPayload() with normalized json and singularized model name</a>
<br>
<h3>Raw Response from server</h3>
<pre>{{pretty-print model.rawAuthor}}</pre>
<br>
<h3>Normalized</h3>
<pre>{{pretty-print authorNormalized}}</pre>
<br>
<h3>Result from model.serialize()</h3>
<pre>{{pretty-print authorSerialized}}</pre>
<br>
{
"version": "0.15.0",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {
"ds-finder-include": true
}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0",
"ember-cli-mirage": "0.4.9",
"ember-lodash": "4.18.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment