Skip to content

Instantly share code, notes, and snippets.

@patrickberkeley
Last active January 8, 2019 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickberkeley/9da9a30b11e6519f19cefcf2175dd2d1 to your computer and use it in GitHub Desktop.
Save patrickberkeley/9da9a30b11e6519f19cefcf2175dd2d1 to your computer and use it in GitHub Desktop.
unloadRecord
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);
}
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();
},
}
});
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'),
});
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",
},
}
};
},
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<button {{action 'save' model}}>Save</button>
<br>
<br>
{
"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