Skip to content

Instantly share code, notes, and snippets.

@buschtoens
Created October 6, 2016 07:32
Show Gist options
  • Save buschtoens/0417b69a9a69b0432f38b8055fa4aa4c to your computer and use it in GitHub Desktop.
Save buschtoens/0417b69a9a69b0432f38b8055fa4aa4c to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
watchMe: null,
_watchMe: null,
updates: Ember.computed(() => []),
didReceiveAttrs() {
const watchMe = this.get('watchMe');
const _watchMe = this.get('_watchMe');
this.set('_watchMe', watchMe);
if (watchMe !== _watchMe) {
this.logUpdate('watchMe', _watchMe, watchMe);
} else {
this.logUpdate('a different property');
}
},
logUpdate(propertyName, from, to) {
this.get('updates').pushObject({
propertyName,
from: String(from),
to: String(to)
});
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
watchMe: 'Hello',
actions: {
triggerGeneralUpdate(propertyName, newValue) {
if (this.get(propertyName) === newValue) {
this.set(propertyName, null);
this.set(propertyName, newValue);
} else {
this.set(propertyName, newValue);
}
},
triggerSpecificUpdate(propertyName, newValue) {
if (this.get(propertyName) === newValue) {
this.set(propertyName, null);
Ember.run.schedule('render', this, this.set, propertyName, newValue);
} else {
this.set(propertyName, newValue);
}
}
}
});
{{input value=watchMe}}
<button onclick={{action "triggerGeneralUpdate" "watchMe" watchMe}}>
Trigger General Update
</button>
<button onclick={{action "triggerSpecificUpdate" "watchMe" watchMe}}>
Trigger Specific Update
</button>
{{my-component watchMe=watchMe}}
<ul>
{{#each updates as |update|}}
<li>
<strong>{{update.propertyName}}:</strong>
{{update.from}} -> {{update.to}}
</li>
{{/each}}
</ul>
{
"version": "0.10.5",
"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.8.0",
"ember-data": "2.8.0",
"ember-template-compiler": "2.8.0",
"ember-testing": "2.8.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment