Here is a working solution, based on Ember Components.
It doesn't handle all edge cases, e.g. bound values that change after the component has rendered, but it does the basics. Can easily be extended if needed.
The array can be constructed using Ember.A()
, in order for it to have addObject
etc.
Component:
App.ArrayCheckboxComponent = Em.Component.extend({
value: null # included in array when checked
array: null # array to bind to
_checked: null # boolean
init: ->
@_super.apply(this, arguments)
array = @get('array')
unless array.addObject && array.removeObject && array.contains
throw new Error('Array used for `ArrayCheckboxComponent` must implement addObject/removeObject')
if array.contains(@get('value'))
@set('_checked', true)
else
@set('_checked', false)
checkedDidChange: (->
value = @get('value')
array = @get('array')
if @get('_checked')
array.addObject(value)
else
array.removeObject(value)
).observes('_checked')
})
Component template:
{{view Em.Checkbox checkedBinding='_checked'}}
Usage:
{{array-checkbox value='item1' arrayBinding='path.to.array'}}
More on components:
http://emberjs.com/guides/components/