Last active
February 8, 2017 16:53
-
-
Save chriskrycho/3f0c4f24f1d85e113f678e4f9b0d8b46 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains 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 { computed, get, set } = Ember; | |
export default Ember.Component.extend({ | |
toChange: undefined, | |
saved: undefined, | |
model: undefined, | |
specificItem: computed.alias('shadowModel.0.items.0.quantity'), | |
_total: computed('shadowModel.@each.itemSum', function() { | |
const groups = get(this, 'shadowModel'); | |
const toSum = (sum, group) => sum + get(group, 'itemSum'); | |
return groups.reduce(toSum, 0); | |
}), | |
anySet: computed.bool('_total'), | |
isDisabled: computed.not('anySet'), | |
firstGroupSum: computed.alias('shadowModel.0.itemSum'), | |
didReceiveAttrs() { | |
const model = get(this, 'model'); | |
const groups = [].concat(model) | |
.map(group => ({ | |
...group, | |
itemSum: computed('items.@each.quantity', function() { | |
const toSum = (sum, item) => sum + parseInt(get(item, 'quantity')); | |
return get(group, 'items').reduce(toSum, 0); | |
}), | |
})); | |
set(this, 'shadowModel', groups); | |
}, | |
actions: { | |
setNested(value) { | |
set(this, 'saved', value); | |
set(this, 'model.0.items.0.quantity', value); | |
} | |
} | |
}); | |
This file contains 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 { computed, get, set } = Ember; | |
export default Ember.Controller.extend({ | |
toChange: undefined, | |
saved: undefined, | |
model: [ | |
{ | |
items: [ | |
{ quantity: 0 }, | |
{ quantity: 0 }, | |
] | |
}, | |
{ | |
items: [ | |
{ quantity: 0 }, | |
{ quantity: 0 }, | |
] | |
}, | |
], | |
actions: { | |
update(value) { | |
set(this, 'model.0.items.0.quantity', value); | |
} | |
} | |
}); |
This file contains 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.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