I’m writing an app with EmberJS v1.0.pre. I have an ArrayController which contains a list of all persons. There are a bunch of nested views showing the person, their pets and the notes for each pet.
|----------------------------------------|
| John | <- Person
|----------------------------------------|
| Quincy (Dog) | <- Pet
| - Super ornery | <- Note
| - Likes XYZ Dog food |
| - Will eat your socks |
| |
| Tom (Cat) |
| - Always (not) catching mice |
| |
|----------------------------------------|
| Roger |
|----------------------------------------|
| V (Dog) |
| - Likes XYZ Dog food |
| - Sneezes, but it's ok |
| |
|----------------------------------------|
| ... |
From the pure MVC standpoint it feels like there should be a controller for each child, but I can’t figure out how to achieve that in Ember. There is the top Array controller and then all the individual views. If I want to delete a note, or edit it, it seems like I need to pass the view’s context up to the controller.
// in the view
click: function () {
this.get('controller').updateNote(this.get('content'))
}
This feels really bad to me, the View is not supposed to be the authoritative source for data. My assumption is that an ArrayController would instantiate an itemControlerClass along with the itemViewClass.
UPDATE: I have created a fiddle to better illustrate my problem. The functionality is intentionally incomplete, the purpose is to finish the functionality by increasing the content when an item on the list is clicked.
UPDATE: Sorry, I deleted the fiddle on accident! I’m doing some work on a final solution, so I’ll try to create a new fiddle w/ the solution.
You don’t need a controller for each item, but instead an ArrayController for each collection of objects (People, Pets, Notes). By the way, any actions on child objects (dogs, notes) shouldn’t be passed to
App.PersonsController. That would break the Separation of Concerns principle.Ember.Router docs cover the case where you want to nest views in a single object ( e.g.
/:people_id). But you want to nest views for an array of objects. I can’t think of a way to nest{{outlet}}views, but you can do the following:Load the objects People, Pets, Notes in 3 ArrayControllers, and delegate actions on child objects to its corresponding ArrayController.
And then your
peopletemplate should look like:As you can see, the actions on child objects are delegated to its controller (pet -> petsController), passing the object as the context. In the case of the
createaction, the controller needs to know to which person the petbelongsTo. Therefore we pass 2 contexts: the person, and the properties of the pet (for simplicity I assumed just a name for the pet).In your App.petsControllers you should have actions along the lines: