2

I need to insert the view & controller from the route categories in multiple other routes, without nesting them in that categories route (because I want to keep URLs independent).

So, I'm rendering the categories template, into the main application one, in the outlet topbar using the controller I get from my existing categories route.

App.PostRoute = Ember.Route.extend({
    renderTemplate: function() {
        this.render();
        this.render('categories', {
            outlet: 'topbar',
            into: 'application',
            controller: this.controllerFor('categories')
        });
    }
});

When I visit the categories route, all's working fine! I can even browse other routes from there. But if I access first any other route, the categories controller seems not to be created:

Assertion failed: The controller for route 'categories'' could not be found. Make sure that this route exists and has already been entered at least once. If you must intialize the controller without entering a route, use `generateController`.

Nice warning :) I wish all frameworks were that smart! So I'm trying to generate that controller manually... But how?

I tried:

App.CategoriesRoute.create().generateController('categories', App.Category)

and the static version:

App.CategoriesRoute.generateController('categories', App.Category)

It doesn't seem to be the right way to do. Any help please?

4

3 回答 3

3

这与我们的朋友IntuitivePixel 一致。

App.PostRoute = Ember.Route.extend({
  renderTemplate: function() {
    this.render();

    //assigning the model...
    var context = ['a','b','c','d'];
    var instance = Em.generateController(this.get('container'),'categories',context);
    instance.set('content',context);

    this.render('categories', {
        outlet: 'topbar',
        into: 'application'
    });
  }
});

您可以在模型钩子本身中执行此操作。

于 2013-08-19T11:01:59.900 回答
2

正如@mavilein 在他的评论中已经提到的那样,这种行为是最近在rc7版本中引入的,遗憾的是this.controllerFor('...');不再为您自动生成相应的控制器,所以我猜通过CategoriesController显式生成(例如在 routesinit方法中)它应该可以工作:

App.CategoriesRoute = Ember.Route.extend({
  init: function() {
    this._super();
    this.generateController('categories');
});

运行示例并检查控制台: http: //jsbin.com/odosoy/62/edit您将看到如下内容:

generated -> controller:categories Object {fullName: "controller:categories"} 

希望能帮助到你。

于 2013-08-19T10:20:53.760 回答
1

要完成来自@seenivasan@IntuitivePixel的答案,这就是我所做的,并且效果很好:

App.PostRoute = Ember.Route.extend({
  init: function() {
    this._super();
    this.generateController('categories');
  },
  renderTemplate: function() {
    this.render();

    //getting the generated controller
    var categoriesController = this.controllerFor('categories');

    //assigning the model
    categoriesController.set('content', App.PostCategory.find());

    this.render('categories', {
        outlet: 'topbar',
        into: 'application',
        controller: categoriesController
    });
  }
});

我认为最好调用函数generateControllerinit确保它只执行一次。

于 2013-08-20T01:17:52.137 回答