Last active
July 21, 2020 21:15
-
-
Save amk221/38bc80958a22a29345f3f3f807f1bae0 to your computer and use it in GitHub Desktop.
Ember Data promise proxy
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 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}` | |
} | |
} | |
} | |
} |
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 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 | |
} | |
} | |
} | |
} |
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 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(); | |
} | |
} |
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'; | |
export default class extends Model { | |
@attr() name; | |
} |
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 } from 'ember-data/relationships'; | |
export default class extends Model { | |
@attr() name; | |
@belongsTo('bar') bar; | |
} |
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 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); | |
} | |
} |
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 RESTSerializer from 'ember-data/serializers/rest'; | |
export default class BarSerializer extends RESTSerializer {} |
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 RESTSerializer from 'ember-data/serializers/rest'; | |
export default class FooSerializer extends RESTSerializer { | |
serialize() { | |
const json = super.serialize(...arguments); | |
console.log('serializing foo', json); | |
} | |
} |
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.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