Action in one controller also affect sibling controllers


#1

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


#2

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.

http://jsfiddle.net/WCSuv/4/

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);


#3

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