7.4 Models -- Pushing Records into the Store

1、概述后端

1. store是做为一个全部records的缓存,这些records已经被你的应用程序加载。在你的app中若是你的路由或者一个controller请求一条record,若是它在缓存中这个store能够当即返回它。不然,这个store必须请求adapter去加载它,这一般意味着从服务器上进行网络访问去检索它。而不是等待应用程序去请求一条record,然而 ,你能够提早把records推送到store的缓存中。缓存

2. 这是有用的,若是你能很好地意识到用户接下来须要什么records。当他们点击一个连接,而不是等待一个网络请求完成,Ember.js能够马上渲染模板。感受是一瞬间的。服务器

3. 推送到records的另外一个用例是若是你的应用程序有一个流链接到后端。若是一条record被建立或者修改,你想当即更新UI。网络

2、Pushing recordsapp

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

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

app/models/album.jscode

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

app/routes/application.js blog

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
    });
  }
});
相关文章
相关标签/搜索