Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concerning the Typical Behavior of Controllers in Ember

Tags:

ember.js

Are controllers in ember.js meant to be tied to main view areas/scenes a la iOS, or more tied to a set of data?

Is it common/wise to have several main views tied to the same controller in ember?

In iOS main portions or sections of the screen are tied to a single controller. If you want to present another main interface, say a modal window to create a new element, you (typically) have an entirely separate controller to manage that view and its data/logic.

In something like Zend Framework, you have controllers that might perform some common spinup steps of ensuring authentication, but largely the actions play the role that controllers do in iOS, handling the logic and providing the data for 1 main section or view (being the web, this usually ends up being the whole page).

What is that typical role or advised pattern for using controllers in ember?

like image 213
Bob Spryn Avatar asked Dec 29 '11 06:12

Bob Spryn


2 Answers

You have a couple different questions here so I'll address them one at a time.

First, you asked if controllers should be data oriented or view oriented. In my experience both behaviors are allowable. Controllers are an excellent way to manage data sets for your application, including things like filtering and searching. Evin Grano wrote a good post about this from the SproutCore perspective and most of the concepts should apply to Ember as well: http://www.itsgotwhatplantscrave.com/2009/07/30/root-controller-paradigm/. Controllers are also well suited for controlling the application state and behavior. For example, you might put a method in a controller that is bound to as a button action elsewhere in your app. However, you should also examine Ember States to see if these might be better suited to your scenario.

Secondly, you asked about tying multiple views to the same controller. Personally, I see no concerns with this as long as the controller maintains a distinct purpose. If the views are logically related and share in the same state or data then a single controller makes sense. If you find the controller growing to cover too many different areas, you should consider splitting it into multiple controllers.

like image 129
Peter Wagenet Avatar answered Sep 19 '22 05:09

Peter Wagenet


From my limited experience in Ember.js, I have regulate to the following:

  • The view handles user actions related to changes in the presentation layer, limited to its own instance.

  • A navigation controller/statemanager handles complex manipulation of what is presented (add multiple views, remove some other, etc.).

  • The controller responds to user actions related to the data layer.

like image 33
Panagiotis Panagi Avatar answered Sep 20 '22 05:09

Panagiotis Panagi