-
-
Save runspired/09f63c8427bc577e240d354b1c4cc5f4 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, get } from '@ember/object'; | |
import { run } from '@ember/runloop'; | |
export default class ApplicationController extends Controller { | |
@action | |
async save() { | |
let props = []; | |
// A fully resolved model | |
props.push(this.foo.name); | |
// A *fulfilled* promise proxy object for a model | |
props.push(get(this.bar, 'name')); | |
// Clear out the relationship | |
run(() => { this.foo.set('bar', null); }); | |
props.push(this.foo.bar && get(this.foo.bar, 'name') ? get(this.foo.bar, 'name') : 'NULL-after-nulled'); | |
props.push(get(this.bar, 'name') ? get(this.bar, 'name') : 'PROXY NULL after nulled'); | |
// Set the relationship back using the | |
// *fullfilled promise proxy object* | |
this.foo.set('bar', this.bar); | |
props.push(this.foo.bar && get(this.foo.bar, 'name') ? get(this.foo.bar, 'name') : 'NULL-after-reset'); | |
props.push(get(this.bar, 'name') ? get(this.bar, 'name') : 'PROXY NULL after reset'); | |
this.set('props', props); | |
// 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 { | |
async model() { | |
return await this.store.findRecord('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.18.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment