Last active
January 8, 2019 18:43
-
-
Save patrickberkeley/9da9a30b11e6519f19cefcf2175dd2d1 to your computer and use it in GitHub Desktop.
Revisions
-
patrickberkeley revised this gist
Jan 8, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ export default Ember.Controller.extend({ // model.save(); set(raw, 'data.id', 1); await this.store.pushPayload('book', raw); const model = this.store.peekRecord('book', get(raw, 'data.id')); this.store.unloadRecord(model); await this.store.pushPayload('book', raw); // model.save(); -
patrickberkeley revised this gist
Jan 8, 2019 . 3 changed files with 49 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ import DS from 'ember-data'; export default DS.JSONAPIAdapter.extend({ async createRecord(store, type, snapshot) { console.log('=====createRecord====='); let payload = await this._super(store, type, snapshot); let id = payload.data.id; // This guard is needed in case the API responds to a create with // an array. We can shim it into Ember Data with `normalize*` hooks // but this hook is hit before the normalize hooks if (id) { let existing = store.peekRecord(type.modelName, id); console.log('id', id); console.log('existing', existing); if (existing) { const existingAttrs = existing.serialize().data.attributes; let newAttrs = payload.data.attributes; await unloadRecord(existing); // assume that since this payload returned later // these attrs are less stale than the existing ones newAttrs = { ...existingAttrs, ...newAttrs }; payload.data.attributes = newAttrs; } } console.log('=====createRecord====='); return payload; }, }) function unloadRecord(model) { const store = model.store; const belongsTo = {}; model.eachRelationship(function(key, relationship) { if (relationship.kind === 'belongsTo') { belongsTo[key] = null; } }); model.setProperties(belongsTo); return store.unloadRecord(model); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,12 +12,13 @@ export default Ember.Controller.extend({ actions: { async save(raw) { console.log('SAVE', raw); // model.save(); set(raw, 'data.id', 1); await this.store.pushPayload('book', raw); const model = this.store.peekRecord('book', get(model, 'id')); this.store.unloadRecord(model); await this.store.pushPayload('book', raw); // model.save(); }, } }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export default Ember.Route.extend({ store: injectService(), model() { // return this.store.createRecord('book', { title: 'Ember Hamster' }); return { "data": { "type": "book", -
patrickberkeley revised this gist
Jan 8, 2019 . 3 changed files with 17 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,19 +2,22 @@ import Ember from 'ember'; import { inject as injectService } from '@ember/service'; import { get, set, } from '@ember/object'; export default Ember.Controller.extend({ appName: 'Ember Twiddle', store: injectService(), actions: { async save(raw) { console.log('SAVE', raw); set(raw, 'data.id', 1); await this.store.pushPayload('book', raw); const model = this.store.peekRecord('book', get(raw, 'data.id'))); debugger this.store.unloadRecord(model); // await this.store.push(model); }, } }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,5 +3,5 @@ import attr from 'ember-data/attr'; import { belongsTo, hasMany } from 'ember-data/relationships'; export default Model.extend({ title: attr('string'), }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,13 @@ export default Ember.Route.extend({ store: injectService(), model() { return { "data": { "type": "book", "attributes": { "title": "Ember Hamster", }, } }; }, }); -
patrickberkeley revised this gist
Jan 8, 2019 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,19 @@ import Ember from 'ember'; import { inject as injectService } from '@ember/service'; import { get, } from '@ember/object'; export default Ember.Controller.extend({ appName: 'Ember Twiddle', store: injectService(), actions: { async save(model) { console.log('SAVE', model); await model.save(); console.log(this.store.peekRecord('book', get(model, 'id'))); this.store.unloadRecord(model); model.save(); }, } -
patrickberkeley revised this gist
Jan 8, 2019 . 4 changed files with 11 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,9 @@ export default Ember.Controller.extend({ appName: 'Ember Twiddle', actions: { save(model) { console.log('SAVE', model); model.save(); }, } }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,4 +3,5 @@ import attr from 'ember-data/attr'; import { belongsTo, hasMany } from 'ember-data/relationships'; export default Model.extend({ name: attr('string'), }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,10 @@ import Ember from 'ember'; import { inject as injectService } from '@ember/service'; export default Ember.Route.extend({ store: injectService(), model() { return this.store.createRecord('book', {name: 'Book 1'}); }, }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,6 @@ <br> <br> {{outlet}} <button {{action 'save' model}}>Save</button> <br> <br> -
patrickberkeley created this gist
Jan 8, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ import Ember from 'ember'; export default Ember.Controller.extend({ appName: 'Ember Twiddle', actions: { save() { console.log('SAVE'); }, } }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ import Model from 'ember-data/model'; import attr from 'ember-data/attr'; import { belongsTo, hasMany } from 'ember-data/relationships'; export default Model.extend({ }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ import Ember from 'ember'; export default Ember.Route.extend({ }); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ <h1>Welcome to {{appName}}</h1> <br> <br> {{outlet}} <button {{action 'save'}}>Save</button> <br> <br> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ { "version": "0.15.1", "EmberENV": { "FEATURES": {} }, "options": { "use_pods": false, "enable-testing": false }, "dependencies": { "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", "ember": "3.4.3", "ember-template-compiler": "3.4.3", "ember-testing": "3.4.3" }, "addons": { "ember-data": "3.4.2" } }