【问题标题】:Ember.js load model for multiple outletsEmber.js 为多个网点加载模型
【发布时间】:2014-08-15 07:33:31
【问题描述】:

我想创建一个类似 UI 的仪表板。出于这个原因,我想在一个页面上呈现多个模板及其各自的模型。

我想,我可以使用 {{render}} 助手解决这个问题(请参阅http://emberjs.com/guides/templates/rendering-with-helpers/#toc_the-code-render-code-helper

我已经渲染了模板,但其中没有数据。 您可以在 JsBin 上查看问题的最小示例http://jsbin.com/vasezutoxege/2/edit?html,css,js,output

JsBin的一些sn-ps与这里的问题直接相关:

App.Router.map(function() {
    this.resource('contacts', function() {
        this.resource('contact', { path: ':contact_id'});
    });
    this.resource('records', function() {
        this.resource('record', { path: ':record_id'});
    });
});

对于每个路由器,我都有一个模型和一个模板。然后我想在我的应用程序的索引路由中显示路由:

<script type="text/x-handlebars" data-template-name="index">
  <!-- link to contacts route is showing contacts template with model right -->
  {{#link-to "contacts"}}Link to contacts route{{/link-to}}
  <!-- this loads the template but without data -->
  <div class="template">
    {{render "contacts" contacts}}
  </div>

  ...

</script>

如何在插座中呈现完整的模板及其模型数据?

感谢您的宝贵时间!

【问题讨论】:

  • 您能否在您的问题中添加相关的代码部分?
  • 嗨,我在帖子中添加了一些代码片段。但也请尝试 JsBin 示例。在那里,您可以在索引路线上看到空的绿色框。这意味着,
      被渲染,但没有从模型中插入

标签: javascript ember.js handlebars.js


【解决方案1】:

你见过http://discuss.emberjs.com/t/dashboard-type-views/5187吗?

在那篇文章中,有多种解决方法的建议。下面的代码似乎可以工作

App.IndexRoute = Ember.Route.extend({
   model: function() {
     return Ember.RSVP.hash({
        contacts: this.store.find('contacts'),
        records: this.store.find('records'),
     });
   },

  setupController: function (controller, context) {
    this.controllerFor('contacts').set('model', context.contacts);
    this.controllerFor('records').set('model', context.records);
  }
});

但是您没有使用 Ember-Data,所以我重新排列它以使用静态数组。这是我的解决方案,仅更改了 javascript。 http://jsbin.com/vasezutoxege/4/edit(它需要小幅更新以满足您的所有需求,尤其是 renderTemplate 中的更多更改)

App.IndexRoute = Ember.Route.extend({    
      setupController: function (controller, context) {
        this.controllerFor('contacts').set('model', CONTACTS);
        this.controllerFor('records').set('model', RECORDS);
      },
    renderTemplate: function() {
        // Render default outlet   
        this.render();
        // render extra outlets
        this.render("records", {
            outlet: "rec",
            into: "index" // important when using at root level
        });
    }  
});
App.IndexController = Ember.ObjectController.extend({
    needs: ['contacts', 'records'] 
});
App.ContactsController = Ember.ArrayController.extend();
App.RecordsController = Ember.ArrayController.extend();

【讨论】:

  • 谢谢你,这就行了。我发现使用 {{render}} 助手也可以,它可以保存 renderTemplate 代码。见这里:jsbin.com/vasezutoxege/5/edit
猜你喜欢
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2014-11-02
相关资源
最近更新 更多