不得不说,Ember的更新是在是太快了!!本教程还没写到一半就又更新到v2.1.0了!!!!不过为了统一仍是使用官方v2.0.0的参考文档!!javascript
从本篇开始进入新的一章——组件。这一章将用6篇文章介绍Ember的组件,从它的定义开始知道它的使用方式,我将为你一一解答!php
准备工做:html
本章代码统一访问项目chapter4_components下,项目代码能够在如下网址上找到:java
https://github.com/ubuntuvim/my_emberjs_codegit
https://git.oschina.net/chendequan/my_emberjs_code_2github
与以前的文章同样,项目仍然是使用Ember CLI命令建立项目和各个组件文件。json
建立项目并测试运行,首先执行以下四条命令,最后在浏览器执行:http://localhost:4200/。ubuntu
ember new chapter4_componentsvim
cd chapter4_components浏览器
ember server
若是你能在页面上看到“Welcome to Ember”说明项目框架搭建成功!那么你能够继续往下看了,不然想搭建好项目再往下学习~~~
建立组件方法很简单:ember generate component my-component-name。一条命令便可,可是须要注意的是组件的名称必需要包含中划线“-”,好比blog-post、test-component、audio-player-controls这种格式的命名是合法,可是post、test这种方式的命名是不合法的!其一是为了防止用户自定义的组件名与W3C规定的元素标签名重复;其二是为了确保Ember能自动检测到用户自定义的组件。
下面定义一个组件,ember g component blog-post。Ember CLI会自动为你建立组件对应的的模板,执行这条命令以后你能够在app/components和app/templates/components下看到建立的文件。
<!-- app/templates/components/blog-post.hbs --> <article> <h1>{{title}}</h1> <p>{{yield}}</p> <p>Edit title: {{input type="text" value=title}}</p> </article>
为了演示组件的使用须要作些准备工做:
ember g route index
// app/routes/index.js import Ember from 'ember'; export default Ember.Route.extend({ model: function() { return [ { id: 1, title: 'Bower: dependencies and resolutions new', body: "In the bower.json file, I see 2 keys dependencies and resolutionsWhy is that so? I understand Bower has a flat dependency structure. So has it got anything to do with that ?", category: 'java' }, { id: 2, title: 'Highly Nested JSON Payload - hasMany error', body: "Welcome to the Ember.js discussion forum. We're running on the open source, Ember.js-powered Discourse forum software. They are also providing the hosting for us. Thanks guys! Please use this space for discussion abo… read more", category: 'php' }, { id: 3, title: 'Passing a jwt to my REST adapter new ', body: "This sets up a binding between the category query param in the URL, and the category property on controller:articles. In other words, once the articles route has been entered, any changes to the category query param in the URL will update the category property on controller:articles, and vice versa.", category: 'java'} ]; } });
<!-- app/templates/index.hbs --> {{#each model as |item|}} <!-- 使用自定义的组件blog-post --> {{#blog-post title=item.title}} {{item.body}} {{/blog-post}} {{/each}}
在这段代码中,使用了自定义的组件来显示数据。最后页面显示以下:
看看生成的HTML代码:
自定义的组件被渲染到了模板index.hbs使用blog-post的地方。而且自定义组件的HTML标签没有变化。
到这里大概应该知道怎么去使用组件了,至于它是怎么就渲染到了使用组件的地方,以及它是怎么渲染上去的。别急~~后面的文章会为你一一解答。
说明:默认状况下,自定义的组件会被渲染到div标签内,固然这种默认状况也是能够修改的,比较简单在此不过多介绍,请自行学习,网址:http://guides.emberjs.com/v2.0.0/components/customizing-a-components-element/。
用户自定义的组件类都须要继承Ember.Component类。
一般状况下咱们会把常用的模板片断封装成组件,只须要定义一次就能够在项目任何一个模板中使用,并且不须要编写任何的javascript代码。好比上述第一点“自定义组件及使用”中描述的同样。
可是若是你想你的组件有特殊的行为,而且这些行为是默认组件类没法提供的(好比:改变包裹组件的标签、响应组件模板初始化某个状态等),那么此时你能够自定义组件类,可是要继承Ember.Component,若是你自定义的组件类没有继承这个类,你自定义的组件就颇有可能会出现一些不可预知的问题。
Ember所能识别的自定义组件类的名称是有规范的。好比,你定义了一个名为blog-post的组件,那么你的组件类的名称应该是app/components/blog-post.js。若是组件名为audio-player-controls那么对应的组件类名为app/components/audio-player-controls.js。即:组件类名与组件同名,这个是v2.0的命名方法,请区别就版本的Ember,旧版本的组件命名规则是驼峰式的命名规则。
举个简单的例子,在第一点“自定义组件及使用”中讲过,组件默认会被渲染到div标签内,你能够在组件类中修改这个默认标签。
// app/components/blog-post.js import Ember from 'ember'; export default Ember.Component.extend({ tagName: 'nav' });
这段代码修改了包裹组件的标签名,页面刷新后HTML代码以下:
能够看到组件的HTML代码被包含在nav标签内。
组件的动态渲染与Java的多态有点类似。{{component}}助手会延迟到运行时才决定使用那个组件渲染页面。当程序须要根据数据不一样渲染不一样组件的时,这种动态渲染就显得特别有用。可使你的逻辑和试图分离开。
那么要怎么使用呢?很是简单,只须要把组件名做为参数传递过去便可,好比:使用{{component ‘blog-post’}}与{{blog-post}}结果是一致的。咱们能够修改第一点“自定义组件及使用”实例中模板index.hbs的代码。
<!-- app/templates/index.hbs --> {{#each model as |item|}} <!-- 使用自定义组件的另外一种方式(动态渲染组件方式) --> {{component 'blog-post' title=item.title}} {{item.body}} {{/each}}
页面刷新以后,能够看到结果是同样的。
下面为读者演示如何根据数据不一样渲染不一样的组件。
按照惯例,先作好准备工做,使用Ember CLI命令建立2个不一样的组件。
ember g component foo-component
ember g component bar-component
<!-- app/templates/components/bar-component.hbs --> <h1>Hello from bar</h1> <p>{{post.body}}</p>
为什么能用post获取数据,由于在使用组件的地方传递了参数。在模板index.hbs中能够看到。
<!-- app/templates/components/foo-component.hbs --> <h1>Hello from foo</h1> <p>{{post.body}}</p>
修改显示的数据,注意数据的最后增长一个属性pn,pn的值就是组件的名称。
// app/routes/index.js import Ember from 'ember'; export default Ember.Route.extend({ model: function() { return [ { id: 1, title: 'Bower: dependencies and resolutions new', body: "In the bower.json file, I see 2 keys dependencies and resolutionsWhy is that so? I understand Bower has a flat dependency structure. So has it got anything to do with that ?", pn: 'bar-component' }, { id: 2, title: 'Highly Nested JSON Payload - hasMany error', body: "Welcome to the Ember.js discussion forum. We're running on the open source, Ember.js-powered Discourse forum software. They are also providing the hosting for us. Thanks guys! Please use this space for discussion abo… read more", pn: 'foo-component' }, { id: 3, title: 'Passing a jwt to my REST adapter new ', body: "This sets up a binding between the category query param in the URL, and the category property on controller:articles. In other words, once the articles route has been entered, any changes to the category query param in the URL will update the category property on controller:articles, and vice versa.", pn: 'bar-component'} ]; } });
修改调用组件的模板index.hbs。
<!-- app/templates/index.hbs --> {{#each model as |item|}} <!-- 根据组件名渲染不一样的组件,第一个参数是组件名,第二个参数为传递到组件上显示的数据 --> {{component item.pn post=item}} {{/each}}
模板编译以后会获得形如{{component foo-component post}}的组件调用代码。
相信你应该了解了动态渲染组件是怎么回事了!本身动手试试吧~~
到此组件的定义与使用介绍完毕了,不知道你有没有学会呢?若是你有疑问请给我留言或者直接看官方教程学习。
文章原网址:http://ibeginner.sinaapp.com/index.php?m=Home&c=Index&a=detail&id=4fd3ad852fa5d701c2b281bdfbe6bfd1