ObjectController and ArrayController

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

I am confused when to use them, I think Em.ObjectController for one object, Em.ArrayController for an array, and Em.Controller only for ApplicationController.

Is there any blessed rule for using that?

+8
source share
2 answers

Usually, if your controller represents a list of elements, you should use Ember.ArrayController , and if the controller represents a single element, you should use 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 must connect them inside the connectOutlets function:

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

Edit: I have never used Ember.Controller . If you look at the source code, it seems that you can use it if you create a custom controller that does not fit into the other two controllers.

+13
source share

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

If the model is an array, you should use an ArrayController. . This will allow you to easily sort or filter in the future. ArrayController typically connects ObjectControllers.

When your model is an instance of Ember Object, you should use an ObjectController. This happens when you use, for example, ember data. With the Objectcontroller, you can directly access model properties. You do not need to write model.property every time.

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

Finally, when you do not have a model, there is an ideal situation for using only Ember.Controller . It does not allow direct access to model properties like an ObjectController.

0
source share

All Articles