Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectController and ArrayController

Tags:

ember.js

I am learning emberjs form trek.github.com. That tutorial used both Em.ObjectController and Em.ArrayController. And There is also Em.Controller.

I am confused when to use them, I guess Em.ObjectController is for single object, Em.ArrayController is for array and Em.Controller is just for ApplicationController.

Is there any blessed rule for when to use which?

like image 552
xnjiang Avatar asked Sep 26 '12 07:09

xnjiang


1 Answers

Usually, if your Controller represent a list of items, you would use the Ember.ArrayController, and if the controller represents a single item, you would use the Ember.ObjectController. Something like the following:

MyApp.ContactsController = Ember.ArrayController.extend({
    content: [],
    selectedContact: null
});

MyApp.SelectedContactController = Ember.ObjectController.extend({
    contentBinding: 'contactsController.selectedContact',
    contactsController: null
});

Then in your Ember.Router (if you use them), you would connect the two inside the connectOutlets function:

connectOutlets: function(router) {
    router.get('selectedContactController').connectControllers('contacts');
}

Edit: I have never used the Ember.Controller. Looking at the source code, it seems like you might want to use this if you are building a custom controller that doesn't fit in with the two other controllers.

like image 100
Joachim H. Skeie Avatar answered Oct 03 '22 23:10

Joachim H. Skeie