为什么这些记录没有存储在缓存中? [英] Why are these records not stored in cache?

查看:14
本文介绍了为什么这些记录没有存储在缓存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to cache my records once they are received, but I can't figure out how. According to the Documentation you can just call this.store.push('model', record), but it doesn't seem to work. Ember requests the data from the server with each call of the route, I would like to do this only once and use the local store after it is fetched from the server.

如果我尝试按照文档的建议进行调试,我知道没有缓存:

If I try to debug it as suggested by the Documentation, i get that there is no cache:

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

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

This is my route (where I try to cache it):

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;
    }
});

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

And this the according Adapter (seems to work fine):

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);
            });
        });
    }
});

推荐答案

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

this.store.find('type') will always make a call to the server for records. If you only want to make a call to the server once do it in the ApplicationRoute and then instead of using find use the all filter inside of the route that's hit multiple times.

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

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

You don't define the primary key on the adapter, it goes on the serializer

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

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

The cache no longer lives there, it lives in this.store.typeMapFor(Pd.Product) or this.store.typeMaps.

在 ember data 1.0 发布之前,该站点仍在引用旧版本的 ember 数据,我假设您使用的是 1.0 测试版.本文档是最新的 https://github.com/emberjs/data/blob/master/TRANSITION.md

The site is still referencing an older version of ember data until ember data 1.0 is released, I'll assume you're using 1.0 beta version. This document is more up to date https://github.com/emberjs/data/blob/master/TRANSITION.md

这篇关于为什么这些记录没有存储在缓存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆