What is the recommended way to use a general view in a template that needs a controller

We use one of the later versions of Ember (Router V2), and not to the edge of the bloodstream with the newer Router V2.2 (Last commit: 668783a (2013-01-06 21:10:55 -0800))

In our solution, we have several View components (Grid, autocomplete, search, etc.). Some of these components refer to the store, models, etc. Therefore, they have a controller that handles this work. These views are used in several templates in our solution.

In the old version (pre2), we used components like this:

App.ConsoleView = Ember.View.extend({ templateName: 'console', searchView: App.SearchView.extend(), ..... }) 

And in the console template, we used a general view like this

 {{view view.searchView controllerBinding='App.searchController'}} 

I always felt that this approach is not the best way, and with the new version of Ember it hit us on the fingers :)

Now to the question: "What is the recommended way to use the general view in a template that needs a controller."

In newer versions of Ember, the expression pattern

 {{view view.searchView controllerBinding='App.searchController'}} 

does not work because App.searchController is no longer created in the application namespace.

I thought about some options, but I really don't like them.

  • I could connect the controller to the "parent controller" through the router, but then I would have to do this on every route where I use a common component, and that will be a lot.
  • I could get the controller through the hacker way and install it through the init function in the init function.

Does anyone have any recommendations on how to do this? I can not find the documentation on this issue and finished googlejuize.

All reviews will be appreciated!

+4
source share
1 answer

I think that I will try to use {{render "search}}}, it will search for SearchController and then create an instance of SearchView and link them.

Otherwise, I know that there is currently a discussion to pass the controller class in a view helper. But not yet implemented.

Update. At the moment, I may be using the second solution you propose using https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/ext/controller.js#L33

 App.ParentView = Ember.View.extend({ searchView = Ember.View.extend({ init: function(){ this._super(); this.set('controller', this.get('parentView.controller').controllerFor('search')) } }) }) 

Here, I assume that all kinds of searches will share the same controller (and its underlying application state).

+2
source

All Articles