课程表

Ember.js 基础

Ember.js 对象模型

Ember.js handlebars模板

Ember.js 路由

Ember.js 组件

Ember.js 控制器

Ember.js 模型

Ember.js 测试

工具箱
速查手册

Ember.js 模型(Model)

当前位置:免费教程 » JS/JS库/框架 » Ember.js

模型是一个用来表示应用程序数据的对象,它可能是一个简单的数组或通过RESTful API动态检索的数据;ember-data.js提供加载、映射和更新应用程序模型的API。

ember-data.js为每个应用程序都提供存储空间,存储空间负责保持已加载的Model和检索还未加载的Model。

前面,我们定义了应用程序App,现在,需要给程序提供数据也就是员工信息,所以我们要创建程序的模型(实体)Employee,接下来我们将实现模型的定义。

  1. // Defines a employee model.
  2. App.Employee = DS.Model.extend({
  3. name: DS.attr('string'),
  4. department: DS.attr('string'),
  5. title: DS.attr('string')
  6. })

上面,我们定义了Employee模型,它继承了DS.Model并且包含三个字段分别是name,department和title。

接下来,我们通过定义App.Employee.FIXTURES,模拟从服务器端获取数据。

  1. // Defines a JSON array.
  2. App.Employee.FIXTURES = [
  3. {
  4. id: 1,
  5. name: 'Jackson Huang',
  6. department: 'IT',
  7. title: 'programmer'
  8. },
  9. {
  10. id: 2,
  11. name: 'Ada Chen',
  12. department: 'purchasing',
  13. title: 'buyer'
  14. },
  15. {
  16. id: 3,
  17. name: 'JK Rush',
  18. department: 'IT',
  19. title: 'programmer'
  20. },
  21. {
  22. id: 4,
  23. name: 'Lucy Liu',
  24. department: 'IT',
  25. title: 'tester'
  26. },
  27. {
  28. id: 5,
  29. name: 'Julia Liu',
  30. department: 'HR',
  31. title: 'Manager'
  32. }
  33. ];

上面,我们定义了JSON数组App.Employee.FIXTURES,它包含了一系列员工的基本信息。

接下来,我们修改home和添加employee模板,具体实现如下:

  1. <!-- Home temp START -->
  2. <script type="text/x-handlebars" data-template-name="home">
  3. <h3>
  4. Employee Information</h3>
  5. <ul>
  6. {{#each item in content}}
  7. <li>{{item}}</li>
  8. {{/each}}
  9. </ul>
  10. <h3>
  11. Employee</h3>
  12. <ul>
  13. {{#each employee in employees}} {{#linkTo "employee" employee}}
  14. <p>
  15. {{employee.name}}
  16. </p>
  17. {{/linkTo}} {{/each}}
  18. </ul>
  19. </script>
  20. <!-- Home temp END -->
  21.  
  22. <!-- Employee temp START -->
  23. <script type="text/x-handlebars" data-template-name="employee">
  24. <div>
  25. <h3>
  26. Name: {{name}}</h3>
  27. <p>
  28. Department: {{department}}
  29. </p>
  30. <p>
  31. Title: {{title}}
  32. </p>
  33. {{#linkTo home class='btn btn-primary'}}Back{{/linkTo}}
  34. </div>
  35. </script>
  36. <!-- Employee temp END -->

在home模板中,我们添加each表达式迭代访问employee元素,然后通过linkTo选择employee路由;然后根据路由选择在employee模板显示相应的员工信息。

程序页面

现在,我们完成了Employee程序的基本功能了,提供用户查下员工的信息了。

以上章节通过Demo例子介绍了Ember的使用,主要介绍了Ember的模型,控制器、模板和路由,由于Ember是Javascript MVC框架,而且作为初学者很容易困惑于它的自动生成和默认规则,所以我极力推荐大家要仔细看一遍RoutingController的官方文档。

我们通过介绍Ember的Handlerbars模板引擎,定义了Demo程序的页面,然后通过路由控制器定义路由行为,根据路由行为选择控制器,控制器负责数据加载和显示。但我们的例子中还没有设计的Ember视图模块,如果想进一步学习请参考官方文档或书籍。

参考

以上四章节出处: http://www.cnblogs.com/rush/

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号