Skip to content

Instantly share code, notes, and snippets.

@nolman
Last active May 22, 2019 05:21
Show Gist options
  • Save nolman/0444a33eabe6fe2c4b76b173176c0451 to your computer and use it in GitHub Desktop.
Save nolman/0444a33eabe6fe2c4b76b173176c0451 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import { computed, set } from '@ember/object';
import { A } from '@ember/array';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
this.set('data', [{value: '123'}]);
},
arrayComputed: computed('data.[].{value}', function() {
return this.get('data').length;
}),
viaLength: computed('data.length', function() {
return this.get('data').length;
}),
eachComputed: computed('data.@each.{value}', function() {
return this.get('data').length;
}),
hashComputed: computed('data.{0,1,2}', function() {
return this.get('data').length;
}),
actions: {
addItemViaSet() {
this.get('data').set(1, {value: 'abc'});
},
changeValueAtIndex1() {
set(this.get('data')[1],'value', 'DEF');
},
notifyPropertyChange() {
this.notifyPropertyChange('data')
},
addItemViaPush() {
this.get('data').pushObject({value: '442'});
},
reset() {
this.set('data', A([{value: '123'}]));
},
},
});
<h1>Welcome to {{appName}}</h1>
<br>
{{#each data as |d|}}
item: {{d.value}}
{{/each}}
<br>
{{outlet}}
<br>
arrayComputed: {{arrayComputed}}
<br>
viaLength: {{viaLength}}
<br>
eachComputed: {{eachComputed}}
<br>
hashComputed: {{hashComputed}}
<br>
<button {{action "addItemViaSet"}}>addItemViaSet</button>
<br>
<button {{action "changeValueAtIndex1"}}>changeValueAtIndex1</button>
<br>
<br>
<button {{action "notifyPropertyChange"}}>notifyPropertyChange</button>
<br><button {{action "addItemViaPush"}}>addItemViaPush</button>
<br><button {{action "reset"}}>reset</button>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment