I am trying to figure out how to create a small application consisting of a list in which you can select several items and switch to select all / none and see the number of selected rows.
I believe that the “selected” state should not be part of the model’s objects, but I cannot figure out how to do this.
This is my current setup (which is not working yet)
Managed Code http://jsfiddle.net/jacobk/rU35G/1/
var App = Ember.Application.create(); App.ApplicationRoute = Ember.Route.extend({ model: function() { return Ember.A(["Foo", "Bar", "Baz"]); } }); App.ApplicationController = Ember.ArrayController.extend({ allSelected: false, selectedCount: function() { return 0; }.property() }); App.RowController = Ember.ObjectController.extend({ isSelected: false });
<script type="text/x-handlebars" data-template-name="application"> <h3>{{ selectedCount }} rows selected.</h3> <label> {{view Ember.Checkbox checkedBinding="allSelected"}} Toggle select all </label> <hr/> <ul> {{#each controller itemController="row"}} <li {{bindAttr class="isSelected"}}> {{view Ember.Checkbox checkedBinding="isSelected"}} {{this.content}} </li> {{/each}} </ul> </script>
- If individual "line items" are controlled using a custom view for each line or a custom controller, as in the script above
- How to distribute "select all" from an ArrayController to all individual controllers (or views, if that works best).
I am trying to figure out when to use bindings, observers, properties, "needs", etc. and when it is advisable to use controllers against representations, etc. I still do not understand the general flow of information / data in ember applications.
eg. should the ArrayController from my example above iterate over the "contained" views / controllers and change the "selected" state when the "select all" checkbox switches OR if all "subcontrollers" observe / "have bindings to the" ArrayController "and change themselves when they change, and if so, how do I spread the data in the opposite direction. How would the ArrayController get the "all selected now" strings?
I would like to see a "canonical solution" for this.
source share