Skip to content

Instantly share code, notes, and snippets.

@rmwxiong
Created February 17, 2017 00:22
Show Gist options
  • Save rmwxiong/3b027188a6873f2bb3e571f9dbf3c448 to your computer and use it in GitHub Desktop.
Save rmwxiong/3b027188a6873f2bb3e571f9dbf3c448 to your computer and use it in GitHub Desktop.
Map to array demo
import Ember from 'ember';
export default Ember.Component.extend({
computedValue: Ember.computed('thing.value', function() {
return this.get('thing.value') + '!!!';
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
mapAsArray: Ember.computed('myMap', function() {
const myMap = this.get('myMap');
const myArray = [];
for (const key in myMap) {
myArray.push({
id: key,
value: myMap[key]
});
}
return myArray
}),
thing: Ember.on('init', function() {
this.set('myMap', {123: 'abc', 456: 'def', 789: 'ghi'});
Ember.run.later(() => {
this.set('myMap', {123: '555', 456: 'def', 789: '999'});
}, 2000);
}),
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{#each mapAsArray as |thing|}}
{{my-component thing=thing}}
{{/each}}
{{outlet}}
<br>
<br>
key: {{thing.id}}
<br>
value: {{thing.value}}
<br>
computedValue: {{computedValue}}
<hr>
{{yield}}
{
"version": "0.11.0",
"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.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment