zoukankan      html  css  js  c++  java
  • 7.4 Models -- Pushing Records into the Store

    一、概述

    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是在路由层次中最顶级的路由,并且当app启动的时候它的model hook会被调用一次。

    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
        });
      }
    });
  • 相关阅读:
    双向链表

    一个简单makefile
    内存管理
    队列
    postgresql表结构查询sql
    postgresql 命令
    NSMutableDictionary 中使用setValueForKey 和 setObjectForKey有什么区别?
    ViewController 生命周期
    程序启动过程
  • 原文地址:https://www.cnblogs.com/sunshineground/p/5165954.html
Copyright © 2011-2022 走看看