简体   繁体   English

Ember.js在静态HTML中呈现多个视图

[英]Ember.js Render multiple views inside static HTML

I'm new to Ember and I'm stuck trying to render views. 我是Ember的新手,我一直在尝试渲染视图。 The application in context is not a single page application but so far Ember has been playing nice until I started dealing with views. 上下文中的应用程序不是单页应用程序,但是到目前为止,Ember在我开始处理视图之前一直表现不错。

I'm trying to render more than one view on a single page (which is like a dashboard and has tons of static HTML and a few containers, one for each view). 我试图在一个页面上呈现多个视图(就像一个仪表板,并且有大量的静态HTML和一些容器,每个视图一个)。

Sample HTML: Let's say I would like to render two lists, one inside the "left-container" div and the other inside the right container. HTML示例:假设我要呈现两个列表,一个在“ left-container” 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>
...

I've tried creating different views and inserting them using the appendTo method (which is described in the Defining a View section of Ember guides) but it throws the error: 我尝试创建不同的视图并使用appendTo方法(在Ember指南的“ 定义视图”部分中进行描述)插入它们,但是会引发错误:

Container was not found when looking up a views template. 查找视图模板时找不到容器。 This is most likely due to manually instantiating an Ember.View. 这很可能是由于手动实例化Ember.View。 See: http://git.io/EKPpnA 请参阅: http//git.io/EKPpnA

and I couldn't find my way using the link that it points to. 而且我找不到使用它指向的链接的方式。

Ember code: 灰码:

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

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

I have also tried using a ContainerView as described in Ember api docs : 我也尝试过使用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');

But I get the same error. 但是我得到了同样的错误。

How should I render each view inside the #left-container and #right-container respectively? 如何分别在#left-container和#right-container内部渲染每个视图? Thanks in advance. 提前致谢。

Template: 模板:

<div id="here"></div>

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

JS: 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 http://emberjs.jsbin.com/nekowidera/1/edit?html,js,输出

It looks like you are trying to make views behave like partials 看起来您正在尝试使视图的行为像局部视图

Move anything static into a partial and include it in your main template like so: 将任何静态内容移动到部分内容中,并将其包含在主模板中,如下所示:

{{partial "header"}}

The lists you want to render can be turned into a component (if the only thing that changes in them is the data). 您想要呈现的列表可以变成一个组件 (如果其中唯一更改的是数据)。

So you end up with a single view that contains partials and components: 因此,您最终得到一个包含零件和零部件的单一视图:

<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>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM