【问题标题】:Why are these records not stored in cache?为什么这些记录没有存储在缓存中?
【发布时间】:2014-05-13 14:26:34
【问题描述】:

我想在收到记录后缓存我的记录,但我不知道怎么做。根据文档,您可以致电this.store.push('model', record),但它似乎不起作用。 Ember 每次调用路由时都会从服务器请求数据,我只想这样做一次,并在从服务器获取数据后使用本地存储。

如果我尝试按照Documentation 的建议对其进行调试,我会发现没有缓存:

Pd.__container__.lookup('store:main').recordCache 
// --> undefined

这是我的路线(我尝试缓存它):

Pd.ProductsRoute = Ember.Route.extend({
    model: function () {
        var promise = this.store.find('product');
        var that = this;
        promise.then(function(value) {

            // Caching supposed to happen here
            value.content.forEach(function(product){
                that.store.push('product', product); 
            });

        }, function(reason) {
            // on rejection
        });
        return promise;
    }
});

这是相应的适配器(似乎工作正常):

Pd.ProductAdapter = DS.RESTAdapter.extend({
    primaryKey: 'nid', // DOES NOT WORK BUT I CAN LIVE WITH THAT (SEE WORKAROUND)

    findAll: function(store, type) {
        var url = 'ws/rest/products';

        return new Ember.RSVP.Promise(function(resolve, reject) {

            jQuery.getJSON(url).then(function(data) {
                Ember.Logger.debug("Received Products:"); // TRIGGERS EVERY TIME!

                var srcPattern = /src=["']([^'"]+)/;

                data.forEach(function(product){
                    product.id = product.nid;
                    product.field_image = srcPattern.exec(product.field_image)[1];
                });

                Ember.Logger.debug(data);

                Ember.run(null, resolve, {product: data});
            }, function(jqXHR) {
                jqXHR.then = null; // tame jQuery's ill mannered promises
                Ember.run(null, reject, jqXHR);
            });
        });
    }
});

【问题讨论】:

    标签: javascript ember.js ember-data


    【解决方案1】:

    this.store.find('type') 将始终调用服务器以获取记录。如果您只想在ApplicationRoute 中调用一次服务器,然后在多次命中的路由中使用all 过滤器,而不是使用find

    Pd.ApplicationRoute = Em.Route.extend({
      model: function(params){
    
        return Em.RSVP.hash({
           product: this.store.find('product'),
           somethingElse: otherPromise
        })
      }
    });
    
    Pd.ProductRoute = Em.Route.extend({
      model: function(params){
        return this.store.all('product');
      }
    });
    

    如果你只是想用你的产品准备商店,你甚至不需要退货,或者在应用程序路线中使用它

    Pd.ApplicationRoute = Em.Route.extend({
      model: function(params){
        this.store.find('product');
        return {foo:'bar'}; // or return nothing, it doesn't matter
      }
    });
    

    延迟加载模型

    App.ProductRoute = Ember.Route.extend({
      hasPreLoaded: false,
      model: function() {
        if(this.get('hasPreLoaded')){
          return this.store.all('product');
        } else {
          this.toggleProperty('hasPreLoaded');
          return this.store.find('product');
        }
      }
    });
    

    示例

    http://emberjs.jsbin.com/OxIDiVU/482/edit

    你没有在适配器上定义主键,它在序列化器上

    Pd.ProductSerializer = DS.RESTSerializer.extend({
      primaryKey: 'nid'
    });
    

    缓存不再存在,它存在于this.store.typeMapFor(Pd.Product)this.store.typeMaps

    在发布 ember data 1.0 之前,该站点仍在引用旧版本的 ember data,我假设您使用的是 1.0 beta 版本。这份文件是最新的https://github.com/emberjs/data/blob/master/TRANSITION.md

    【讨论】:

    • 你,先生,真棒!如果您只想按需加载它,即在调用 ProductRoute 时,但从那时起从缓存中加载,您将如何进行?
    • 抛出了一个延迟加载的例子,如果你在pretty colorsHome 之间切换它会多次命中路由。我第一次让模拟响应花一秒钟,这样你就可以看到它在等待乐趣。
    • 像魅力一样工作!这比花几个小时浏览文档更有帮助。
    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2017-08-21
    • 1970-01-01
    • 2014-06-11
    相关资源
    最近更新 更多