I got the following WorkSpec item controller, which controls checkboxes state:
ContractorApp.WorkSpecController = Em.ObjectController.extend
needs: ['account']
selected: (->
work_spec = @get 'content'
work_specializations = @get 'controllers.account.work_specializations'
work_specializations.contains work_spec
).property()
selection_changed: (->
work_spec = @get 'content'
work_specializations = @get 'controllers.account.work_specializations'
limit = @get 'controllers.account.specialization_limit'
if @get( 'selected' ) && work_specializations.get('length') < limit
work_specializations.pushObject work_spec
else
work_specializations.removeObject work_spec
).observes('selected')
As you see work_specializations is the parent's set of work specializations: I want to control (work_specializations.get('length') < limit) the addition of an element to the array.
The problem here that I got observer in the model contained the work_specializations array:
ContractorApp.ContractorProfile = DS.Model.extend
work_specializations: DS.hasMany "work_specialization"
on_spec_change: ( ->
ws_ids = @get( 'work_specializations').map (ws)->
ws.get 'id'
@set( 'work_specialization_ids', ws_ids )
).observes 'work_specializations'
For some reason the call of 'work_specializations.pushObject work_spec' calls the model's callback but its counterpart removeObject does not. Any ideas why?
You're observing the collection itself, not items being added or removed. It's likely that the first change is triggering the observes to fire for the first time.
on_spec_change: ( ->
ws_ids = @get( 'work_specializations').map (ws)->
ws.get 'id'
@set( 'work_specialization_ids', ws_ids )
).observes 'work_specializations.[]'