Multiple instances of the same controller at the same time in Ember

Just watched the Ember Peepcode video. One thing he scored for me is that the controllers are singleton, so at runtime one instance of each controller is created, and the controller data property will change to input / output as needed.

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

How does Amber handle this situation?

+6
source share
2 answers

There are several ways to deal with this (mentioned in a previous answer).

Method 1:

{{render}} using the model (requires the latest build of Ember.js):

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

Method 2:

July 7, 2014 Patch: {{control}} 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 downloading the ember.js file.

There is also an error that you will need to fix:

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

Method 3:

Using {{each}} with itemController .

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

Each of them creates a new separate instance each time.

+5
source

I found that if you override the controller's init method and configure the content on a new object (or whatever you use), you can have several independent instances.

 App.MyMultipleController = Ember.ObjectController.extend({ content: {}, init: function(){ this.set('content', {}); return this._super.call(this, arguments); } }); 

Here's the JSFiddle. It uses ObjectProxy for my own purposes, but you can use ObjectController if you want: http://jsfiddle.net/jgillick/D83d8/

0
source

All Articles