Last active
January 8, 2019 18:43
-
-
Save patrickberkeley/9da9a30b11e6519f19cefcf2175dd2d1 to your computer and use it in GitHub Desktop.
unloadRecord
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({ | |
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 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 { 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); | |
// 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(); | |
}, | |
} | |
}); |
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 Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import { belongsTo, hasMany } from 'ember-data/relationships'; | |
export default Model.extend({ | |
title: attr('string'), | |
}); |
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 { inject as injectService } from '@ember/service'; | |
export default Ember.Route.extend({ | |
store: injectService(), | |
model() { | |
// return this.store.createRecord('book', { title: 'Ember Hamster' }); | |
return { | |
"data": { | |
"type": "book", | |
"attributes": { | |
"title": "Ember Hamster", | |
}, | |
} | |
}; | |
}, | |
}); |
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.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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment