Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active April 13, 2021 23:51
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 runspired/15740f376ac1cbed140e3265663e9084 to your computer and use it in GitHub Desktop.
Save runspired/15740f376ac1cbed140e3265663e9084 to your computer and use it in GitHub Desktop.
Serialize Changed Relationships
export default class AppAdapter {
constructor(args) { Object.assign(this, args); }
static create(args) { return new this(args); }
updateRecord(store, schema, snapshot) {
const posts = snapshot.record.hasMany('posts').hasManyRelationship;
const remote = posts.canonicalMembers.copy();
const local = posts.members.copy();
console.log({ remote, local });
return;
}
}
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
});
import { helper } from '@ember/component/helper';
export default helper(function v(params/*, hash*/) {
switch (params[0]) {
case undefined:
return '[[UNDEFINED]]';
case null:
return '[[NULL]]';
default:
return params[0];
}
});
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'),
posts: hasMany('post', { inverse: 'author', async: false }),
});
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'),
author: belongsTo('author', { inverse: 'posts', async: false }),
});
import Ember from 'ember';
function makePost(id, addAuthor = true) {
return {
type: 'post',
id,
attributes: { title: `post number ${id}` },
relationships: {
author: { data: addAuthor ? { type: 'author', id: '1' } : null }
}
}
}
export default Ember.Route.extend({
async model() {
const { store } = this;
const author = store.push({
data: {
type: 'author',
id: '1',
attributes: { name: 'Chris' },
relationships: {
posts: {
data: [
{ type: 'post', id: '1' },
{ type: 'post', id: '2' },
{ type: 'post', id: '3' },
{ type: 'post', id: '4' },
{ type: 'post', id: '5' },
]
}
}
},
included: [
makePost('1'),
makePost('2'),
makePost('3'),
makePost('4'),
makePost('5'),
]
});
const newPost = store.createRecord('post', {
title: 'new post',
});
const otherPost = store.push({ data: makePost('6', false) });
const post4 = store.peekRecord('post', '4');
// remove a post
author.posts.removeObject(post4);
// add new post
author.posts.pushObject(newPost);
// update with a new post
author.posts.pushObject(otherPost);
await author.save();
return author;
}
});
<h3>posts</h3>
<ul>
{{#each this.model.posts as |post|}}
<li>{{post.id}} {{post.title}}</li>
{{/each}}
</ul>
{
"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.16.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment