It seems like you're coming from a jQuery background because of how you're trying to embed the id
of your ClassGroup records into the DOM via the template (nothing wrong with that of course as that's what I was doing when I first started with Ember coming from a jQuery heavy background). If you notice yourself doing this you're probably doing something in a non-ember way. Generally, you should only be accessing DOM elements by ID if you need to integrate some 3rd party jQuery library.
Okay, so now you know what to look out for how should we accomplish what you want in the Ember way?
There are probably several ways to go about it but what works well for me is to create another controller for each individual ClassGroup that extends ObjectController instead of ArrayController. So you would have
ClassGroupsController (ArrayController) -> ClassGroupController (ObjectController)
Notice that the array controller is pluralized. What this allows you to do is to manage the state of each individual ClassGroup record because each ClassGroup record now has its own controller and its own view.
So First create an array controller that manages the collection of ClassGroups:
App.ClassGroupsController = Ember.ArrayController.extend({
});
The ClassGroupController (Object) should then have a view which listens for click events. When we get a click event we trigger an action on the controller (ClassGroupController) called selected
.
App.ClassGroupView = Ember.View.extend({
templateName: 'classGroup',
/*
By specifying the tagName for this view we are telling ember
we want to wrap the view in a <tr> element. This is because
each classGroup is going to be a row in our table. By default
each view is wrapped in a <div>
*/
tagName: 'tr',
/*
This is the click handler that is called whenever the user
clicks on this view (in this case one of our <tr> elements
that is displaying our ClassGroup Model for us. The event
parameter is the jQuery event object.
*/
click: function(event){
/*
We want to forward this event to the controller where
we can do some logic based on the fact that it was clicked.
*/
this.get('controller').send('selected');
}
});
Now we create our ObjectController for ClassGroup which is waiting and listening for the selected
action which is being sent from the ClassGroupView
when the user clicks on that individual ClassGroup
.
App.ClassGroupController = Ember.ObjectController.extend({
actions:{
/*
This is the action we received from our view which was
based on the user clicking on one of the <li> elements
representing a ClassGroup Model
*/
selected: function(){
console.info("You selected: " + this.get('className'));
/*
Now we can easily toggle the isActive state of our model
*/
this.toggleProperty('isActive');
console.info("My isActive state is now: " + this.get('isActive'));
}
}
});
Now we need to connect this up in our template.
{{#each itemController="classGroup"}}
{{view App.ClassGroupView}}
{{/each}}
Notice how I specify that I want to use the classGroup as an itemController for my ArrayController. Here I specify it directly in the template. You can also specify it in the ArrayController itself using the itemController
property. So for example:
App.ClassGroupsController = Ember.ArrayController.extend({
itemController: 'classGroup'
});
Here is a working JSFIddle using your fixtures to demonstrate the entire concept: