Skip to content

Instantly share code, notes, and snippets.

@Kilowhisky
Last active February 3, 2021 06:11
Show Gist options
  • Save Kilowhisky/4a4344310f79688a836b8f6fb4b57f11 to your computer and use it in GitHub Desktop.
Save Kilowhisky/4a4344310f79688a836b8f6fb4b57f11 to your computer and use it in GitHub Desktop.
Issue with ObjectPromiseProxy
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { ObjectPromiseProxy, resolvingPromise, resultObject } from '../utils/object-proxy';
export default class ApplicationController extends Controller {
regularObject = resultObject;
//@computed() // uncomment this to get it to work 'somehow'
get promiseObj() {
return ObjectPromiseProxy.create({
promise: resolvingPromise(resultObject)
});
}
get promiseObjQuick() {
return ObjectPromiseProxy.create({
promise: resolvingPromise(resultObject, 0)
});
}
// This is really to support something like this
// Which cannot be in a route model hook
// get myModel() {
// return this.store.findRecord('model', 1);
//}
// If i happened to be using an array ember-data has a solution that works
// instances: computed('userIds.[]', function () {
// return DS.PromiseArray.create({
// promise: all(this.userIds.map(u => this.store.find('user', u)))
// });
// }),
}
import Controller from '@ember/controller';
import { computed } from '@ember/object';
import { ObjectPromiseProxy, resolvingPromise, resultObject } from '../utils/object-proxy';
export default Controller.extend({
regularObject: resultObject,
promiseObj: computed(function() {
return ObjectPromiseProxy.create({
promise: resolvingPromise(resultObject)
});
}),
promiseObjQuick: computed(function() {
return ObjectPromiseProxy.create({
promise: resolvingPromise(resultObject, 0)
});
})
});
<h1>ObjectPromiseProxy &amp; Octane Failure</h1>
<br>
<p>This twiddle will show that the classic controller will properly cause the UI to re-render when the promises resolve whereas the octane ones will not re-render at all.</p>
<br>
{{outlet}}
<strong>Octane</strong> <br/>
<label>no promise: </label> {{regularObject.name}} <br/>
<label>promise (5s): </label> {{promiseObj.name}} <br/>
<label>promise (0s): </label> {{promiseObjQuick.name}} <br/>
<br>
<br>
<strong>Classic</strong> <br/>
<label>no promise: </label> {{regularObject.name}} <br/>
<label>promise (5s): </label> {{promiseObj.name}} <br/>
<label>promise (0s): </label> {{promiseObjQuick.name}} <br/>
{
"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"
}
}
import ObjectProxy from '@ember/object/proxy';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import EmberObject from '@ember/object';
import { Promise } from 'rsvp';
import { later } from '@ember/runloop';
export const ObjectPromiseProxy = ObjectProxy.extend(PromiseProxyMixin);
export const resultObject = EmberObject.create({
name: 'loaded'
});
export function resolvingPromise(result, timeout = 5000) {
return new Promise(resolve => {
later(this, () => resolve(result), timeout);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment