Skip to content

Instantly share code, notes, and snippets.

@amk221
Last active July 21, 2020 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amk221/38bc80958a22a29345f3f3f807f1bae0 to your computer and use it in GitHub Desktop.
Save amk221/38bc80958a22a29345f3f3f807f1bae0 to your computer and use it in GitHub Desktop.
Ember Data promise proxy
import RESTAdapter from 'ember-data/adapters/rest';
export default class FooAdapter extends RESTAdapter {
ajax(path, method, options) {
const id = path.match(/\d+/)[0];
return {
bar: {
id: id,
name: `Bar${id}`
}
}
}
}
import RESTAdapter from 'ember-data/adapters/rest';
export default class FooAdapter extends RESTAdapter {
ajax(path, method, options) {
return {
foo: {
id: 1,
name: 'Foo 1',
bar: 1
}
}
}
}
import Controller from '@ember/controller';
import { action } from '@ember/object';
export default class ApplicationController extends Controller {
@action
save() {
// A fully resolved model
console.log('foo', this.foo);
// A *fulfilled* promise proxy object for a model
console.log('foo.bar', this.bar);
// Clear out the relationship
this.foo.set('bar', null);
// Set the relationship back using the
// *fullfilled promise proxy object*
this.foo.set('bar', this.bar);
// Save...
this.foo.save();
}
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default class extends Model {
@attr() name;
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo } from 'ember-data/relationships';
export default class extends Model {
@attr() name;
@belongsTo('bar') bar;
}
import Route from '@ember/routing/route';
import { hash } from 'rsvp';
export default class ApplicationRoute extends Route {
model() {
return this.store.find('foo', 1);
}
setupController(controller, model) {
controller.foo = model;
// Resolve bar ahead of time for this example.
controller.bar = controller.foo.bar;
// This fixes the issue, but why?
// controller.bar = this.store.findRecord('bar', 1);
}
}
import RESTSerializer from 'ember-data/serializers/rest';
export default class BarSerializer extends RESTSerializer {}
import RESTSerializer from 'ember-data/serializers/rest';
export default class FooSerializer extends RESTSerializer {
serialize() {
const json = super.serialize(...arguments);
console.log('serializing foo', json);
}
}
Click save and check the console.<br>
Try this with ED 3.10 and then ED 3.18<br><br>
Then also look at the conment in routes/application.js
<br><br>
<button type="button" {{on "click" this.save}}>
Save
</button>
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "3.10.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment