【问题标题】:Ember.js Render multiple views inside static HTMLEmber.js 在静态 HTML 中渲染多个视图
【发布时间】:2014-10-16 01:44:16
【问题描述】:

我是 Ember 的新手,我一直在尝试渲染视图。 上下文中的应用程序不是单页应用程序,但到目前为止,Ember 一直很好,直到我开始处理视图。

我试图在单个页面上呈现多个视图(这就像一个仪表板,有大量静态 HTML 和一些容器,每个视图一个)。

示例 HTML: 假设我想渲染两个列表,一个在“左容器”div 内,另一个在右容器内。

<div class="static-header></div>
<!-- more static content goes here -->
<div id="left-container"></div>
<!-- more static content goes here -->
<div id="right-container"></div>
...

我尝试使用 appendTo 方法(在 Ember 指南的 Defining a View 部分中描述)创建不同的视图并插入它们,但它会引发错误:

查找视图模板时找不到容器。这很可能是由于手动实例化了 Ember.View。见:http://git.io/EKPpnA

我无法使用它指向的链接找到我的方式。

Ember 代码:

var App = Ember.Application.create({});
App.HomeViewLeft = Ember.View.extend({    
    templateName: 'home-left',
});

var view = App.HomeViewLeft.create();
view.appendTo('#left-container');

我也尝试过使用 Ember api docs 中描述的 ContainerView:

App.HomeViewLeft = Ember.View.extend({    
    templateName: 'home-left',
});

var containerView = Ember.ContainerView.create({
classNames: ['container-view'],
});

containerView.pushObject(App.HomeViewLeft.create());
containerView.appendTo('left-container');

但我得到了同样的错误。

我应该如何分别在#left-container 和#right-container 中渲染每个视图? 提前致谢。

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    模板:

    <div id="here"></div>
    
    <script type="text/x-handlebars" data-template-name="components/my-foo">
      {{text}}
    </script>
    

    JS:

    App = Ember.Application.create({});
    
    Ember.Application.initializer({
      name: 'stand-alone-components',
    
      initialize: function(container, application) {
        App.MyFooComponent.create({text: 'Hello World', container: container}).appendTo('#here');
      }
    });
    
    App.MyFooComponent = Ember.Component.extend({
      init: function() {
        this._super();
        this.set('layout', Ember.TEMPLATES['components/my-foo']);
      }
    });
    

    http://emberjs.jsbin.com/nekowidera/1/edit?html,js,output

    【讨论】:

      【解决方案2】:

      您似乎正在尝试使视图的行为类似于 partials

      将任何静态内容移动到部分中,并将其包含在您的主模板中,如下所示:

      {{partial "header"}}
      

      您要呈现的列表可以转换为component(如果其中唯一改变的是数据)。

      所以你最终会得到一个包含部分和组件的视图:

      <div class="static-header>{{partial "header"}}</div>
      {{partial "static-content"}}
      <div id="left-container">{{list-component data=firstList}}</div>
      {{partial "static-content"}}
      <div id="right-container">{{list-component data=secondList}}</div>
      ...
      

      【讨论】:

      • 谢谢。我会试试这个。是否可以不将静态内容移动到模板中?它的内容太多了,将其留在正文中然后添加任何动态内容会感觉更自然。是否可以以编程方式执行此操作?
      • 将静态内容移动到部分只是为了可读性,您可以保留它
      • 我可能没有说清楚。我指的是不使用主模板并将其全部保留在我页面的 标记中。这样我就不需要将所有静态内容提取到模板中。那可能吗?谢谢。
      • 你可以,但你为什么会呢?您确定 ember 是适合您的工具吗?如果将它们放入主体中,它们将在整个站点中持续存在。
      • 问题是应用不是单页应用,是传统的多页应用,所以静态内容只会出现在当前页面。到目前为止,我所有的应用程序逻辑都大大减少了,但视图变得有点棘手。如果我把静态内容放在页面的正文中,我怎么能“注入”动态内容呢?为所有的询问道歉,但我对 Ember 很陌生。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 2014-02-14
      • 2016-05-19
      • 2012-03-04
      相关资源
      最近更新 更多