Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Instances of the Same Controller Simultaneously in Ember

Just been watching the Ember Peepcode video. One thing it has hammered home to me is that Controllers are singletons, so a single instance of each Controller is created at runtime and the controller's data property is swapped in/out as needed.

But what happens when you need multiple versions of the same controller on screen and active at the same time. What happens if I have multiple example.handlebars templates, each of which needs to be backed by its own version of ExampleController on screen simultaneously?

How does Ember handle this situation?

like image 291
Undistraction Avatar asked Mar 29 '13 14:03

Undistraction


1 Answers

There are several ways to handle that (mentioned in my previous answer).

Method 1:

{{render}} with a model (needs latest Ember.js build):

{{render "example" example1}}
{{render "example" example2}}

Method 2:

Update July 7 2014: {{control}} has been removed from Ember >= 1.0.

{{control}} (it is still buggy so avoid if you can)

{{control "example"}}

But first you need to enable the flag: ENV.EXPERIMENTAL_CONTROL_HELPER = true before loading ember.js file.

There's also a bug which you'll need to fix by doing:

App.register('controller:example', App.ExampleController, {singleton: false }

Method 3:

Using {{each}} with itemController.

{{#each controller itemController="example"}}
  {{view "example"}}
{{/each}}

Each of these will create a new separate instance every time.

like image 97
Teddy Zeenny Avatar answered Oct 11 '22 15:10

Teddy Zeenny