What’s the best practice in the situation when there is array of objects (for example Contacts) handled by App.contactsController and each of the contacts has array of Messages related to him?
The approaches that came to my mind:
1) Create messagesController for each contact when initializing the object and pushing it to the contactsController, like this:
App.contactsController.pushObject(App.Contact.create({
"id" : some_id,
"name" : some_name,
"messages" : App.MessagesController.create();
}));
2) Instantiate only one messagesController for the application and filter the messages somehow. This seems to be really bad solution, but it’s definitely an option.
3) Don’t use ArrayController at all. But why am I using the framework when I don’t use its features?
Please, help me to decide. If there is anything better, I am ready to follow your opinion. Just explain the advantages (and possible pitfalls) of your preferred solution, please.
Controllers are best suited for managing application state. Most of the example i’ve seen proxy to either a single object or an array, so the framework classes make sense as a starting point. I’ve not seen anyone use them to manage associations. For that, I’d suggest having a look at ember-data: https://github.com/emberjs/data
If you’re going to do this without using ember-data, I’d recommend a modified version of choice #1. You’d still gave ContactsController which extends ArrayController, but dont use another controller for the internal array of messages. Instead go with either an internal array of simple objects or perhaps a list of message_ids that reference instances of an App.Message model.
Hope this helps…