Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sagelliv/9a1252d03cfd9ad5cb8c5f825b29b4ef to your computer and use it in GitHub Desktop.
Save sagelliv/9a1252d03cfd9ad5cb8c5f825b29b4ef to your computer and use it in GitHub Desktop.
array not updating
import Ember from 'ember';
import {
times,
constant
} from 'lodash';
const {
get,
set,
Component,
on,
computed,
A: EArray,
} = Ember;
export default Component.extend({
answers: null,
answerCheckedStates: null,
onInit: on(`init`, function () {
const lengthAmount = this.get(`answers.length`)
const answerCheckedStates = times(lengthAmount, () => false)
this.set(`answerCheckedStates`, answerCheckedStates)
}),
checkedStates: computed.mapBy('answers', 'checked'),
actions: {
toggleStatus(index) {
const currentValue = get(this, `answerCheckedStates.${index}`)
console.log(`Current value`, currentValue)
set(this, `answerCheckedStates.${index}`, !currentValue)
const answer = get(this, 'answers').objectAt(index);
set(answer, 'checked', !currentValue);
const changedValue = get(this, `answerCheckedStates.${index}`)
console.log(`Changed value`, changedValue)
}
}
});
import Ember from 'ember';
const {
A: EArray,
} = Ember;
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
answers: EArray([
{ id: 1, value: `Tomato`, checked: false },
{ id: 2, value: `Oranges`, checked: false },
{ id: 3, value: `Pineapple`, checked: false },
{ id: 4, value: `Sushi`, checked: false },
])
});
import Ember from 'ember'
export function nthChild(params/*, hash*/) {
return params[0][params[1]]
}
export default Ember.Helper.helper(nthChild)
{{some-component
answers=answers
}}
{{#each-in answers as |index answer|}}
{{#paper-checkbox
value=(nthChild checkedStates index)
onChange=(action "toggleStatus" index)
}}
{{answer.value}}
{{/paper-checkbox}}
{{/each-in}}
{
"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",
"ember-lodash": "4.17.4",
"ember-paper": "1.0.0-alpha.19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment