为什么这些记录不存储在缓存中
Why are these records not stored in cache?
我想缓存我的记录一旦他们收到,但我不知道如何。根据文档,您可以调用this.store.push('model', record)
,但它似乎不起作用。Ember通过每次调用路由从服务器请求数据,我希望只这样做一次,并在从服务器获取数据后使用本地存储。
如果我尝试调试它按照建议的文档,我得到没有缓存:
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);
});
});
}
});
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测试版。本文档是最新的https://github.com/emberjs/data/blob/master/TRANSITION.md
相关文章:
- 将脚本缓存到本地存储的basket.js概念仍然是一个好主意吗
- “DOMException:找不到条目”时将大型响应放入缓存存储
- HTML5 本地存储作为缓存和单个资产请求
- 如何在浏览器中存储缓存
- 如何使用jquery将ajax响应存储在浏览器缓存中
- 将此 pdf 保存在 ionic 上的缓存/本地存储中
- AngularJS 工厂函数无法访问本地存储的缓存变量
- 如果我存储缓存并且已达到缓存限制会发生什么
- 如何验证“缓存控制”、“无缓存、无存储、必须重新验证”
- HTML5应用程序缓存文件存储在客户端的位置
- 缓存 Web 应用程序界面并使用 html5 本地存储恢复它
- Extjs 4 存储 - 查询整个存储,而不仅仅是缓存的记录
- 客户端存储/缓存大型HTML
- 将图像存储在浏览器缓存中-加载图像的最佳方式
- 显示缓存中存储的内容
- 如何在HTML5'中存储50MB视频;s应用程序缓存
- HTML5本地存储VS应用缓存离线网站浏览
- 如何在iOS中存储UIWebView缓存,使其在页面打开时不会闪烁
- 为什么这些记录不存储在缓存中
- 服务辅助角色的缓存存储的大小限制是多少