8

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?

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My understanding is that you use Ember.Controller when the controller is a not a proxy (neither for an object nor for an array) like in the case of your App.ApplicationController.
Joachim pauldechov Thanks for your precise reply! Now I know the difference of them.
0

The general rule is that it depends on model from route.

If model is an array then you should use ArrayController. It will allow you to implement in easy way sorting or filtering in future. ArrayController is connecting usually ObjectControllers.

When your model is an instance of Ember Object then you should use ObjectController. It takes place when you are using for instance ember data. With Objectcontroller you can access model properties directly. You don't have to write model.property each time.

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return Ember.Object.create({name: 'Mathew'});
    }
});

My name is {{name}}

Finally, when one doesn't have model there is an ideal situation to use just Ember.Controller. It is not going to allow direct access to model properties as ObjectController.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.