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

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

问题描述

我希望在接收到记录后缓存我的记录,但我无法弄明白。根据文档,您可以调用 this.store.push('model',record),但它似乎不起作用。 Ember在每次呼叫路由时从服务器请求数据,我只想这样做一次,并在从服务器取出后使用本地商店。



如果我按照文档的建议尝试调试,我得到没有缓存:

  Pd .__ container __。lookup('store:main')。recordCache 
// - >未定义

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

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

//缓存应该发生在这里
value.content .forEach(function(product){
that.store.push('product',product);
});

},function(reason){
//拒绝
});
返回承诺;
}
});

这个根据适配器(似乎工作正常):

  Pd.ProductAdapter = DS.RESTAdapter.extend({
primaryKey:'nid',//不工作,但我可以这样做(查看替代方法)

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

返回新的Ember.RSVP.Promise (解析,拒绝){

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

Ember.run(null,resolve,{product:data});
},function(jqXHR){
jqXHR.then = null; // tame jQuery的恶劣承诺
Ember.run(null,reject,jqXHR);
});
});
}
});


解决方案

this.store.find ('type')将始终调用服务器进行记录。如果您只想在 ApplicationRoute 中执行此操作,而不是使用 find 使用$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ .ApplicationRoute = Em.Route.extend({
model:function(params){

return Em.RSVP.hash({
product:this.store.find产品'),
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'}; //或什么都不返回,没关系
}
});



懒惰加载模型



  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('产品');
}
}
});



示例



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



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

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

缓存不再生活在这里,它住在 this.store.typeMapFor (Pd.Product) this.store.typeMaps



该网站是仍然引用旧版本的ember数据,直到ember数据1.0被发布,我假设你使用的是1.0 beta版本。本文档是最新的 https://github.com/emberjs/data/ blob / master / TRANSITION.md


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

If you just want to prep the store with your products, you don't even need to return it, or use it in the app route

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

Lazy loading the models

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

Example

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

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

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天全站免登陆