Emberjs 将多个控制器合二为一 [英] Emberjs loading multiple controllers into one

查看:14
本文介绍了Emberjs 将多个控制器合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景
我目前有一个 IndexRoute.我想将其他 3 个控制器加载到其中.这 3 个控制器分别称为 SportsNewsBusiness.我阅读了 embersjs 文档,它指出您需要将 renderTemplate 钩子实现到 IndexRoute 中,以便在索引中显示每个控制器.

SCENARIO
I currently have an IndexRoute. I want to load 3 other controllers into it. Those 3 other controllers are called Sports, News, Business. I read the embersjs documentation and it states that you need to implement the renderTemplate hook into the IndexRoute for each of those controllers to be shown in the index. http://emberjs.com/guides/routing/rendering-a-template/

Once I did that I put in the index template

{{ outlet sports }}
{{ outlet news }}
{{ outlet business }}

They are showing up but as I look through the EmberInspector extension the individual model for Sports, News, Business are not loading.

QUESTION
Why are the models for these Sports, News, Business not loading in the index?

SEE JSBIN for my code sample
http://jsbin.com/gecarido/1/edit

ATTACHED IMAGE

解决方案

route's are only hit when you define and hit a route via url.

For example if you'd defined your router like this:

Ember.Router.map(function(){
  this.resource('foo', function(){
    this.resource('bar');
  });
});

And hit /foo/bar

It would hit

App.FooRoute = Em.Route.extend({

});

and

App.BarRoute = Em.Route.extend({

});

If you want to hit it all from just the root url you might as well return it all from the application model hook.

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return {
      colors: ['red', 'yellow', 'blue'],
      news: ['Europe', 'Asia', 'America'],
      business: ['Markets', 'Finance', 'Stocks'],
      sports: ['golf', 'hockey', 'football']
    };
  }  
});

And then you can use render from the template and supply it a template name and a model.

<script type="text/x-handlebars">
  <h2>Welcome to Ember.js</h2>

  <ul>
  {{#each item in colors}}
    <li>{{item}}</li>
  {{/each}}
  </ul>

  <br>
  {{render 'sports' sports}}
  <br>
  {{render 'news' news}}
  <br>
  {{render 'business' business}}
  <br>
  {{outlet}}
</script>

http://jsbin.com/gecarido/3/edit

这篇关于Emberjs 将多个控制器合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆