Last active
April 13, 2021 23:51
-
-
Save runspired/15740f376ac1cbed140e3265663e9084 to your computer and use it in GitHub Desktop.
Serialize Changed Relationships
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
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; | |
} | |
} |
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 service } from '@ember/service'; | |
export default Ember.Controller.extend({ | |
}); |
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 { 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]; | |
} | |
}); |
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'), | |
author: belongsTo('author', { inverse: 'posts', async: false }), | |
}); |
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'; | |
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; | |
} | |
}); |
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.16.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment