Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nested save
import { Promise, defer } from 'rsvp';
let newID = 1;
export default class AppAdapter {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
/*
generateIdForRecord(store, modelName, properties) {
return `${modelName}:${newID++}-${properties.name}`;
}
*/
prepareNestedSave(record) {
let promise = defer();
let final = record.save({
adapterOptions: {
isNestedChild: true,
promise
}
});
return { final, deferred: promise };
}
createRecord(store, ModelClass, snapshot) {
let isNestedChild = snapshot.adapterOptions.isNestedChild === true;
if (isNestedChild) {
return snapshot.adapterOptions.promise.promise;
}
let shouldDoNestedSave = snapshot.adapterOptions.nestedSave === true;
let user = {};
user.attributes = snapshot.attributes();
user.type = snapshot.modelName;
user.id = snapshot.id || null;
let fatherSnapshot = snapshot.belongsTo('father');
let father = {};
father.attributes = fatherSnapshot.attributes();
father.type = fatherSnapshot.modelName;
father.id = fatherSnapshot.id || null;
let fatherPromises = this.prepareNestedSave(fatherSnapshot.record);
return new Promise(resolve => {
setTimeout(resolve, 1500);
})
.then(() => {
user.id = user.id === null ? newID++ : user.id;
father.id = father.id === null ? newID++ : father.id;
fatherPromises.deferred.resolve({ data: father });
return fatherPromises.final.then(() => {
return { data: user };
});
});
}
}
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this._super(...arguments);
let store = this.get('store');
let user = store.createRecord('person', { name: 'Chris' });
let father = store.createRecord('person', { name: 'John' });
user.set('father', father);
this.user = user;
user.save({ adapterOptions: { nestedSave: true, nestedProps: ['father'] }});
}
});
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(),
father: belongsTo('person', {
async: false,
inverse: null }),
friends: hasMany('person', {
async: false,
inverse: 'friends'
})
});
export default class MySerializer {
constructor(options) {
Object.assign(this, options);
}
static create(options) {
return new this(options);
}
normalizeResponse(_, __, data) { return data; }
}
<h1>Welcome to {{appName}}, {{user.name}}</h1>
My Id {{user.id}}.<br>My state is: <b>{{user.currentState.stateName}}</b>
<br>
{{user.father.name}} is your father. His ID is {{user.father.id}}.<br>His state is: <b>{{user.father.currentState.stateName}}</b>
<br>
{{outlet}}
<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
You can’t perform that action at this time.