Skip to content

Instantly share code, notes, and snippets.

@jmimi
Forked from moranje/controllers.application.js
Last active March 8, 2017 19:47
Show Gist options
  • Save jmimi/0d4f239ea72586492455f5cdf5181faf to your computer and use it in GitHub Desktop.
Save jmimi/0d4f239ea72586492455f5cdf5181faf to your computer and use it in GitHub Desktop.
nutrient-total
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
list: [],
actions: {
add() {
this.get('list').pushObject({
ingredient: {
name: 'Potato',
group: 'Veggies',
nutrients: [{
name: 'kcal',
nutritionalValue: 87
}, {
name: 'kJ',
nutritionalValue: 42
}]
},
weight: 42
});
this.notifyPropertyChange('list');
}
}
});
import Ember from 'ember';
const {get} = Ember;
export function nutrientTotal([list, unit]) {
let total = 0;
list.forEach(item => {
let weight = get(item, 'weight');
get(get(item, 'ingredient'), 'nutrients').forEach(nutrient => {
if (get(nutrient, 'name') === unit) {
total += weight * get(nutrient, 'nutritionalValue');
}
});
});
return total;
}
export default Ember.Helper.helper(nutrientTotal);
<table style="width:100%">
<tr>
<th>Group</th>
<th>Name</th>
<th>Weight</th>
</tr>
{{#each list as |item|}}
<tr>
<td>{{item.ingredient.group}}</td>
<td>{{item.ingredient.name}}</td>
<td>{{item.weight}}g</td>
</tr>
{{/each}}
</table>
<br>
<button {{action "add"}}>Add</button>
<br>
<br>
{{nutrient-total list 'kcal'}}
{
"version": "0.11.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.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.11.0",
"ember-testing": "2.11.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment