在调用时使用查询参数缓存记录? Ember公司数据 [英] Cache a record when using Query Params in the call? Ember-data
问题描述
我有这条路线检索2个型号:
I have got this route retrieving 2 models:
App.PanelRoute = Ember.Route.extend({
model: function(){
var topologymin = this.store.find('topologymin');
var metricmap = this.store.find('metricmap', { param1: 'something'})
return Ember.RSVP.hash({
topologymin: topologymin,
metricmap: metricmap
});
});
这会产生2个电话:
http://localhost/topologymins
http://localhost/metricmaps?param1=something
如果我再去另一条路线,那么再次使用params而不是另一条路线:
If I go to another route and again to this one, it makes again the call with the params, not the other one:
http://localhost/metricmaps?param1=something
但是,因为同样的调用来检索相同的记录,我希望它们像另一个调用一样被缓存。
But, as its the same call to retrieve the same records I would like them to be cached like in the other call.
它如何知道何时调用服务器,何时这不是必需的?是否可以这样做?
How does it know when to call the server and when its not necessary? Is it possible to do that?
我的型号:
App.Topologymin = DS.Model.extend({
siteGroup: DS.attr('string'),
sites: DS.hasMany('site')
});
App.Metricmap = DS.Model.extend({
profile: DS.attr('string'),
link: DS.attr('string'),
services: DS.attr()
});
推荐答案
当您根据参数发出请求时,Ember Data doesn不知道这些参数是否会转化为模型(也就是说它不知道你拥有所有的记录有某种关系param1)。您可以自己缓存,但是您仍然需要某种方法来了解您商店中其他记录中的记录。
When you fire a request based on params Ember Data doesn't know how those params necessarily translate into the models (aka it doesn't know that you have all of the records that have some sort of relationship param1). You can cache it yourself, but then you'd still need some sort of way of knowing those records from other records in your store.
App.PanelRoute = Ember.Route.extend({
model: function(){
var met = this.get('fetchedBeforePromise'),
topologymin = this.store.find('topologymin'),
metricmap = met || this.store.find('metricmap', { param1: 'something'});
this.set('fetchedBeforePromise', metricmap);
return Ember.RSVP.hash({
topologymin: topologymin,
metricmap: metricmap
});
});
这篇关于在调用时使用查询参数缓存记录? Ember公司数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!