Skip to content

Instantly share code, notes, and snippets.

@workmanw
Last active December 30, 2015 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save workmanw/9caf25254d6d35c436c2 to your computer and use it in GitHub Desktop.
Save workmanw/9caf25254d6d35c436c2 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
const DEFAULT_MODEL = {
relationship: {
isFulfilled: false,
someValue: false
}
}
export default Ember.Controller.extend({
appName:'Ember Twiddle',
model: Ember.copy(DEFAULT_MODEL, true),
actions: {
changeModel() {
this.set('model', Ember.copy(DEFAULT_MODEL, true));
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
{{input type="checkbox" checked=model.relationship.isFulfilled}} isFulfilled<br>
{{input type="checkbox" checked=model.relationship.someValue}} someValue<br>
<br>
<button {{action "changeModel"}}>Change Model</button>
<br><br><br>
{{#if (async-not model.relationship 'someValue')}}
Showing some value
{{else}}
Not showing some value
{{/if}}
// app/helpers/async-value.js
import Ember from 'ember';
const {
get,
addObserver,
removeObserver
} = Ember;
export default Ember.Helper.extend({
installObserver(relationship, key) {
if(this._installObsRelationship !== relationship || this._installObsKey !== key) {
this.uninstallObserver();
this._installObsRelationship = relationship;
this._installObsKey = key;
addObserver(this._installObsRelationship, this._installObsKey, this, this.recompute);
addObserver(this._installObsRelationship, 'isFulfilled', this, this.recompute);
}
},
uninstallObserver() {
if(this._installObsRelationship && this._installObsKey) {
removeObserver(this._installObsRelationship, this._installObsKey, this, this.recompute);
removeObserver(this._installObsRelationship, 'isFulfilled', this, this.recompute);
this._installObsRelationship = null;
this._installObsKey = null;
}
},
willDestroy() {
this.uninstallObserver();
},
compute([asyncRelationship, key], hash) {
this.installObserver(asyncRelationship, key);
return asyncRelationship && !get(asyncRelationship, key) && get(asyncRelationship, 'isFulfilled');
}
});
{
"version": "0.4.17",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.2.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment