Skip to content

Instantly share code, notes, and snippets.

@buschtoens
Last active July 20, 2017 10:26
Show Gist options
  • Save buschtoens/2d505ae4881ff5703611586fc5e57543 to your computer and use it in GitHub Desktop.
Save buschtoens/2d505ae4881ff5703611586fc5e57543 to your computer and use it in GitHub Desktop.
Computed Property at runtime
import Ember from 'ember';
const {
computed: { collect, reads },
defineProperty,
mixin,
propertyDidChange,
propertyWillChange,
run: { later },
set
} = Ember;
function forceSet(obj, keyName, value) {
propertyWillChange(obj, keyName);
defineProperty(obj, keyName, value);
propertyDidChange(obj, keyName);
return value;
}
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
country: 'America',
description: 'land of the free',
init() {
later(() => {
// set(this, 'appName', collect('country', 'description'));
// defineProperty(this, 'appName', collect('country', 'description'));
// mixin(this, { appName: collect('country', 'description') });
// this.appName = collect('country', 'description'); // Assertion Failed: You must use Ember.set() to set the...
forceSet(this, 'appName', collect('country', 'description'));
}, 2000);
later(() => {
set(this, 'description', 'land of the cinnabon');
}, 4000);
later(() => {
// set(this, 'appName', reads('country')); // crashes
// defineProperty(this, 'appName', reads('country'));
// mixin(this, { appName: reads('country') });
// this.appName = reads('country'); // Assertion Failed: You must use Ember.set() to set the...
forceSet(this, 'appName', reads('country'));
}, 6000);
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{
"version": "0.12.1",
"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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment