Created
September 6, 2018 06:02
-
-
Save sdhull/ea8b22e6fef7572ed3e233952c309e44 to your computer and use it in GitHub Desktop.
push vs pushPayload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
export default DS.JSONAPIAdapter.extend({ | |
namespace: '/api' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
// } | |
}, | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export function prettyPrint([json]/*, hash*/) { | |
return JSON.stringify(json, null, 2); | |
} | |
export default Ember.Helper.helper(prettyPrint); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
} | |
} | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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