Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Thoburn runspired

View GitHub Profile
View controllers.application\.js
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
let renderCount = 0;
function trackedWithCache(target, key, desc) {
let _v;
let d = tracked(target, key, desc);
return {
View adapters.application\.js
import JSONAPIAdapter from '@ember-data/adapters/json-api';
// ideally write entirely your own thing, but you can do this just as well extending an existing adapter
export default class Adapter extends JSONAPIAdapter {
async query(store, schema, query, recordArray, { adapterOptions }) {
if (adapterOptions.isRelationshipQuery) {
// tbh depending on this link in this situation is probably not the way
// to go. You probably want to construct the url yourself or access the full
// set of links and choose the right one.
@runspired
runspired / adapters.application\.js
Last active Apr 13, 2021
Serialize Changed Relationships
View adapters.application\.js
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 });
View adapters.post.js
import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default class PostAdapter extends JSONAPIAdapter {
async findRecord(store, type, id) {
switch (id) {
case '1':
return {
"data": {
"id": "1",
"type": "posts",
"links": {
View controllers.application\.js
import Controller from '@ember/controller';
import { tracked, cached } from '@glimmer/tracking';
function cachedWithKey(invalidationKey) {
return function(target, key, descriptor) {
let _v;
let hasComputed = false;
let cacheddesc = cached(target, key, {
get() {
this[target][key];
View controllers.application\.js
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import EmberObject, { action, computed, observer } from '@ember/object';
import { dependentKeyCompat } from '@ember/object/compat';
class Foo extends EmberObject {
@tracked _tag = '';
@dependentKeyCompat
get id() {
View controllers.application\.js
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { computed, action } from '@ember/object';
import { dependentKeyCompat } from '@ember/object/compat';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@tracked _tag = 0;
View adapters.application\.js
export default class Adapter {
constructor(args) { Object.assign(this, args); }
static create(args) { return new this(args); }
findRecord(store, schema, id, snapshot) {
let type = schema.modelName;
console.log({ type, id }, snapshot);
if (type === 'post') {
let authorId = snapshot.belongsTo('author', { id: true });
View adapters.application\.js
import { inject } from '@ember/service';
const MAPPINGS = {
'user': 'v1',
'posts': 'json-api',
}
export class DelegatingAdapter {
@inject store;
View controllers.application\.js
import Controller from '@ember/controller';
import LinkedList from '../linked-list';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
data = LinkedList.create();
}