Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use which controller type?

Tags:

ember.js

I seem to be getting more and more confounded at what appears, on the surface at least to be pretty basic architectural questions regarding building ember apps.

Which Controller type?

In the last month or so, I've seen people implement controllers through Ember.Controller, Ember.ArrayController, Ember.ObjectController, and Ember.ArrayProxy. Removing ArrayController and ArrayProxy (due to them being identical), what are common use cases between each type?

So far, i've been able to gather that:

  • ArrayControllers/Proxies should be used when you have n elements within the view you intend to control
  • ObjectControllers should be used when the view is simple enough to maintain it's state in a single object, or be a single instance of a model's object.
  • Controllers --- ? No idea.

What are some basic differences between the controller types? There doesn't seem to be concrete information on when to use which, and for which use case. The API docs are good at telling me the nitty gritty of each of them, but not WHEN to use each.

The relationship between a View and a Controller can be baffling

When a View is connected via a routes ConnectOutlets function call, what exactly happens between the controller and the view?

Are events tied into the view itself (which seems to be the case) and if so, where on earth do you interact with the controller singleton to perform CRUD-esq things on its properties? this.get('controllerName') doesn't seem to do the trick, and nearly each post or tutorial or code sample out there does this a different way.

Models that aren't

I realize that Ember Data looks to help solve some of the more irritating parts of dealing with data and keeping it in sync, but at a larger perspective, in the concept of "MVC", ember doesn't really seem to have a Model of any kind. It's just some object that gets subclassed from a specific thing and then tracked....somewhere? Somehow? Magical?

@trek sufficed that an Ember.Object could manage ajax'ing data and handling state on the client just fine, but if you look at something like the todomvc.com ember app, it uses a localStorage paradigm that is COMPLETELY different in implementation then everything i've looked at.

How EXACTLY should the 'Model' part of the MVC equation be done here?

Views make me want to murder children

There seems to be a significant number of ways to construct a "view" in terms of displaying markup to a user.

  • ContainerViews, using subviews / childviews
  • nested outlets
  • Handlebars templates + an outlet
  • using #each foo in controller
  • Injection through literals (template: Ember.Handlebars.compile('<h1>foo</h1>') etc)

With that in mind, what's the 'proper' way to build modular UI components with ember? This more than anything is a major pain point for the adoption of this framework for me.

I love the direction that Ember is going with application development on the web. The concepts seem simple, but the verbosity is that of Objective-C (which makes sense given it's lineage) but I swear to god I feel like i'm fighting the god damned framework more than i'm actually working on my application. The verbosity of the syntax and the lack of structured documentation outside of API documentation (which lets face it, 300k of javascript is a significant amount of code to throw some breakpoints down and try to debug your issues).

I realize the challenge that you guys are up against, but hopefully this at least makes you pause for a minute and think of how you could make life easier for the incoming developer who's worked with other frameworks (or hell, even worked within an MVC framework, like rails or django or backbone or angular) and say "this is how we think ember should be used".

Take some of the opinionated software design decisions and apply them toward the community. We'll do nothing but be cheerleaders for you if you do it, promise.

like image 628
Thock Avatar asked Dec 12 '12 00:12

Thock


People also ask

How do I choose a controller?

Some hardware requirements to consider are the amount of memory, scan-time speed, and battery backup. The controller will need sufficient system memory to support both data and program requirements. Determining how many devices need to be supported by the system helps with the required data memory estimates.

What are three types of controllers?

These three types of controllers can be combined into new controllers: Proportional and integral controllers (PI Controller) Proportional and derivative controllers (PD Controller) Proportional integral derivative control (PID Controller)

When would you use a proportional controller?

Proportional control is used where maintaining a process variable to a tighter tolerance and timely responsiveness are required. Control systems in many industrial settings as well as some smart devices use proportional control. Proportional control involves fine-grained control through a feedback mechanism.


1 Answers

Please don't hurt any children. AFAIK the ember-core team are all over 18, so any ember-view-related frustration is clearly better directed towards adults. With that in mind...

Which Controller Type?

You've got the "what" right, but maybe missing the "why". Controller can be a little misleading, especially coming from rails. Think of these controller singletons as representing the state (in-memory) of your application.

Which kind of controller to use depends on what is required for that part of your application. For example, a back-of-napkin sketch for any app might have a topnav, postList, postDetails section. In ember, each is represented by one or more view/controller pairs. In this app I would expect to see ApplicationController and NavigationController extending Ember.Controler while postList would extend ArrayController and PostDetails would be an ObjectController.

You could do it all using just Ember.Controller but ObjectController and ArrayController are really useful for wrapping model data. Any non-trivial ember app will probably use all three.

The relationship between a View and a Controller

A controller's job is to provide the context in which the view will be rendered. Ideally you'd like to keep logic out of views, so a typical controller will have lots of computed properties to do things like:

  • transform data from the underlying model objects
  • sort/filter/select a list of objects
  • reflect application state

whats the deal with connectOutlets? This is where you should be using the requested route/context to decide which views/data should be plugged into the outlets of your application. The controller's connectOutlet method has a bunch of magic to make it easy, but maybe too much magic. What happens (afaik) when you call: parentcontroller.connectOutlet 'child' is

  • Ember creates an instance of ChildView
  • The {{outlet}} handlebars helper in parentController's view is bound to this childView instance
  • The childView is rendered with the router.childController singleton as it's context

where to do crud stuff?: Typically in an action on the router. This seems crazy at first. Think of ember router not like rails but as a stateManager that just happens to also handle routing. In near-future router API will change to make this more clear. Anyway, use router actions to do things like create model instances, commit/rollback transactions and trigger state change. This is easy to do if you use the handlebars {{action}} helper for links/buttons as it targets the router by default.

Views on the other hand should have logic for "reacting to browser events" - that means really low-level stuff like show/hide something on mouseover or integrate with 3rd party libraries to do effects and animations.

You might find this screencast helpful in understanding how to do CRUD-esq things: http://blog.bigbinary.com/2012/09/06/crud-application-in-emberjs.html

Models WTF?

Agreed in Ember any object could be used as a 'Model'. I think @trek does a good job of demonstrating how one might accomplish this via Ember.Object. This works great for a simple app, and six months back maybe would've been your best bet as ember-data was really immature. I'm not clear on the history of ember's todomvc app, but for sure it was written months ago. For sure it should be updated, but meantime I'd not recommend using it to learn about current ember best-practices.

Instead, you should go with ember-data. Over the last few months it has really evolved and should be the default choice for any new, non-trivial ember app. @tomdale just gave a great presentation on this topic, I'd recommend having a look: https://speakerdeck.com/tomdale/ember-data-internals

what's the 'proper' way to build modular UI components with ember?

For building modular UI components:

  • ContainerViews, using subviews / childviews
  • Injection through literals (template: Ember.Handlebars.compile ...)

For building an individual application:

  • nested outlets
  • Handlebars templates + an outlet
  • using #each foo in controller

Building modular UI components is a totally different problem than building an application. Ember.View and it's subclasses were designed for this purpose. You can easily extend/combine them to compose widgets with custom behaviors and share those widgets across applications.

At least that's how i've seen it done. If they are for internal use could also reference handlebars templates instead of object literals, but if planning to distribute the object literals approach seems best.

A great real-world example of this is the ember-bootstrap project. I learned a lot about working with ember-views by reading through that project's source. http://emberjs-addons.github.com/ember-bootstrap/

TLDR

  • Pick controller that maps to type of data being represented
  • Controllers provide context for the view and remember application state
  • Use ember data for your models
  • Use subclasses of Ember.View to make components
  • Be nice to children
like image 95
Mike Grassotti Avatar answered Jan 04 '23 02:01

Mike Grassotti