Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
Ember.computed.async = function (...args) {
// Remove computed function to call later
const func = args.pop();
// Add new custom computed function that returns promise proxy
args.push(function(propName) {
// Call original computed function
const promise = Ember.RSVP.resolve().then(() => func.call(this, propName));
const ObjectPromiseProxy = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin);
return ObjectPromiseProxy.create({
promise: promise,
});
});
return Reflect.apply(Ember.computed, this, args);
};
Ember.computed.async.prorotype = Ember.computed.prototype;
export default Model.extend({
name: attr('string'),
promise: Ember.computed('name', () => {
const name = this.get('name');
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(name);
}, 1000);
});
return promise;
}),
asyncComputed: Ember.computed.async('promise', () => {
console.log(arguments);
console.log(this);
}),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.get('store').createRecord('application', {
name: "Alex",
});
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
Name {{model.name}} <br>
{{input value=model.name}}
<br>
<br>
{{#if model.asyncComputed}}
Computed is true
{{else}}
Computed is false
{{/if}}
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2"
},
"addons": {}
}
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.