Action in one controller also affect sibling controllers

I’m trying to set up a simple Item system where only one Item can be selected at a time. If you select an item, the previously selected one should be deselected.

App.ItemController = Ember.ObjectController.extend({
    selected: false,
    actions: {
	select: function() {
		this.set('selected', true);
		//now I want to .set('selected', false) to all other Items.
	 }
     }
});

App.ItemsController = Ember.ArrayController.extend({

    //Do I need this Items Array controller to accomplish this?
    //If so, how would I deselect them all here?

});

I’m not sure exactly how to access sibling ItemControllers or how to have the array ItemsController affect all its children.

Another option I considered was using ‘selected’ as an attribute on the model, but without the DS so that detail doesn’t touch the server. Though, it feels a little unneccessary to clutter up the model for a state thing like this.

Thanks a ton for any help,

Drew

I wrote an example awhile back for this so I’m just going to re-paste it. It handles arrowing, so just disregard that portion.

Edit: In short, if you wanted to set all of the item controllers selected property to false within the ArrayController, you just need to this.setEach('selected', false);

1 Like

Thanks a ton! This fiddle is very helpful. Really appreciate it.