Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save RuslanZavacky/6e5ee339506bad8a4402f9af99382cb9 to your computer and use it in GitHub Desktop.

Select an option

Save RuslanZavacky/6e5ee339506bad8a4402f9af99382cb9 to your computer and use it in GitHub Desktop.
Testing Computed Properties
import Ember from 'ember';
import set from 'ember-metal/set';
import computed from 'ember-computed';
import run from 'ember-runloop';
export default Ember.Component.extend({
flagValue: computed(() => []),
hashValue: computed('flagValue.@each.value', function() {
console.log('hashValue: ', this.get('flagValue'));
return this.get('flagValue');
}).volatile(),
didReceiveAttrs() {
this.set('flagValue', [0, 0, 0]);
},
actions: {
clickM() {
let value = Math.random() > 0.5 ? 0 : 1;
console.log('>>>>>> click, value: ', value);
let a = this.get('flagValue');
let copy = a.slice();
a[0] = value;
let isEqual = true;
if (a.length !== copy.length) {
isEqual = false;
} else {
for (let i = 0; i < a.length; i++) {
if (a[i] !== copy[i]) {
isEqual = false;
break;
}
}
}
if (!isEqual) {
this.set('flagValue', a);
this.notifyPropertyChange('flagValue');
}
console.log('a', copy, a, a[0], isEqual);
},
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{my-component}}
<br>
<br>
{{yield}}
<button onclick={{action 'clickM'}}>
click here to generate a random 1 or 0
</button>
<br><br>
{{#each hashValue as |value|}}
hashValue: {{value}}
{{/each}}
<br><br>
{{#each flagValue as |value|}}
flagValue: {{value}}
{{/each}}
{
"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