Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
init() {
this._super(...arguments);
this.set('myArray', []);
},
getNewItem() {
const items = this.get('items');
return Math.floor(Math.random() * 100);
},
myItems1: Ember.computed('myArray', function() {
return this.get('myArray').join(', ');
}),
myItems2: Ember.computed('myArray.[]', function() {
return this.get('myArray').join(', ');
}),
actions: {
addItemToArray() {
this.get('myArray').addObject(this.getNewItem());
},
addItemAndReplaceArray() {
const myArray = this.get('myArray');
const newArray = myArray.concat([this.getNewItem()]);
this.set('myArray', newArray);
},
},
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
<h1>computed property lists demo</h1>
<br>
<br>
<p>Try using "Add Item" and "Add and replace" below. Notice that when using "Add Item", only the computed property thas uses `.[]` updates, and when using replace, they both update.</p>
<p>
<button {{action 'addItemToArray'}}>Add Item</button> | <button {{action 'addItemAndReplaceArray'}}>Add and replace</button>
</p>
<p>These are the items: {{myItems1}}</p>
<p>These are the items with .[]: {{myItems2}}</p>
<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
You can’t perform that action at this time.