首页 > 其他 > 详细

7.4 Models -- Pushing Records into the Store

时间:2016-02-01 15:39:02      阅读:189      评论:0      收藏:0      [点我收藏+]

一、概述

1. store是作为一个所有records的缓存,这些records已经被你的应用程序加载。在你的app中如果你的路由或者一个controller请求一条record,如果它再缓存个中这个store可以立即返回它。否则,这个store必须请求adapter去加载它,这通常意味着从服务器上进行网络访问去检索它。而不是等待应用程序去请求一个条record,然而 ,你可以提前把records推送到store的缓存中。

2. 这是有用的,如果你能很好地意识到用户接下来需要什么records。当他们点击一个链接,而不是等待一个网络请求完成,Ember.js可以立刻渲染模板。感觉是一瞬间的。

3. 推送到records的另一个用例是如果你的应用程序有一个流连接到后端。如果一条record被创建或者修改,你想立即更新UI。

二、Pushing records

1. 调用store的push()方法来推送一条record到store。

2. 例如,假设当应用程序第一次启动时,我们想提前加载一些数据到store中。我们可以使用route:application来这样做。route:application是在路由层次中最顶级的路由,并且它的model hook会被调用一次当app启动的时候。

app/models/album.js

export default DS.Model.extend({
  title: DS.attr(),
  artist: DS.attr(),
  songCount: DS.attr()
});

app/routes/application.js

 

export default Ember.Route.extend({
  model() {
    this.store.push(‘album‘, {
      id: 1,
      title: "Fewer Moving Parts",
      artist: "David Bazan",
      songCount: 10
    });

    this.store.push(‘album‘, {
      id: 2,
      title: "Calgary b/w I Can‘t Make You Love Me/Nick Of Time",
      artist: "Bon Iver",
      songCount: 2
    });
  }
});

7.4 Models -- Pushing Records into the Store

原文:http://www.cnblogs.com/sunshineground/p/5165954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!