Ember 设置记录到Store

2018-01-06 18:00 更新

EmberStore就像一个缓存池,用户提交的数据以及从服务器获取的数据会首先保存到Store。如果用户再次请求相同的数据会直接从Store中获取,而不是发送HTTP请求去服务器获取。

当数据发生变化,数据首先更新到Store中,Store会理解更新到其他页面。所以当你改变Store中的数据时,会立即反应出来,比如上一篇更新记录小结,当你修改article的数据时会立即反应到页面上。

1,push()方法

你可以调用push()方法一次性把多条数据保存到Store中。比如下面的代码:

  1. // app/routes/application.js
  2. import Ember from 'ember';
  3. export default Ember.Route.extend({
  4. model: function() {
  5. this.store.push({
  6. data: [
  7. {
  8. id: 1,
  9. type: 'album',
  10. attributes: { // 设置model属性值
  11. title: 'Fewer Moving Parts',
  12. artist: 'David Bazan'
  13. songCount: 10
  14. },
  15. relationships: {} // 设置两个model的关联关系
  16. },
  17. {
  18. id: 2,
  19. type: 'album',
  20. attributes: { // 设置model属性值
  21. title: 'Calgary b/w I Can\'t Make You Love Me/Nick Of Time',
  22. artist: 'Bon Iver',
  23. songCount: 2
  24. },
  25. relationships: {} // 设置两个model的关联关系
  26. }
  27. ]
  28. });
  29. }
  30. });

注意type属性值必须是模型的属性名字。attributes哈希里的属性值与模型里的属性对应。

本篇不是很重要,就简单提一提,如有兴趣请看Pushing Records Into The Store的详细介绍。


博文完整代码放在Github(博文经过多次修改,博文上的代码与github代码可能有出入,不过影响不大!),如果你觉得博文对你有点用,请在github项目上给我点个star吧。您的肯定对我来说是最大的动力!!

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号