Created
March 11, 2017 15:58
-
-
Save moranje/257eea5db4ba89f9ba5500a1279cf64d to your computer and use it in GitHub Desktop.
nutrient-total-helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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