[英]ember-data - store.find('model') always queries the server
Details: ember-data-1.0.0.beta.3 and the default RESTAdapter 详细信息:ember-data-1.0.0.beta.3和默认的RESTAdapter
I might have misunderstood how the store.find()
method works, but, from my understanding, the following code should not query the server if the records I'm asking for are already present in the store: 我可能误解了
store.find()
方法是如何工作的,但是,根据我的理解,如果我要求的记录已存在于商店中,则以下代码不应查询服务器:
var IndexRoute = Em.Route.extend({
model: function() {
return this.store.find('link');
},
});
From the emberjs.com documentation for DS.Store.find()
: 来自
DS.Store.find()
的emberjs.com文档:
The find method will always return a promise that will be resolved with the record.
find方法将始终返回将使用记录解析的promise。 If the record was already in the store, the promise will be resolved immediately.
如果记录已经在商店中,则承诺将立即得到解决。 Otherwise, the store will ask the adapter's find method to find the necessary data.
否则,商店将询问适配器的find方法以查找必要的数据。
I have another route with the exact same model hook, but when I visit that route, and even though the data is already in the store, the server gets queried. 我有另一条路径与完全相同的模型钩子,但当我访问该路线时,即使数据已经存储在商店中,服务器也会被查询。 And if I go back to the Index route, it gets queried again.
如果我回到Index路线,它会再次被查询。 Shouldn't
.find()
handle this? 不应该
.find()
处理这个吗?
The find method will always return a promise that will be resolved with the record.
find方法将始终返回将使用记录解析的promise。 If the record was already in the store, the promise will be resolved immediately.
如果记录已经在商店中,则承诺将立即得到解决。 Otherwise, the store will ask the adapter's find method to find the necessary data.
否则,商店将询问适配器的find方法以查找必要的数据。
This just work when finding by id this.store.find('link', 1)
. 这通过id
this.store.find('link', 1)
查找this.store.find('link', 1)
。 Using this.store.find('link')
will always perform requests in the server. 使用
this.store.find('link')
将始终在服务器中执行请求。
You can get the local data using the all
method this.store.all('link')
. 您可以使用
this.store.all('link')
的all
方法获取本地数据。 But in some place of your app, you will need to preload that data using the find
method. 但是在应用程序的某个位置,您需要使用
find
方法预加载该数据。 Otherwise all
will return nothing. 否则
all
不会返回。
You can use the following to get the desired behavior: 您可以使用以下方法获得所需的行为:
App.ApplicationRoute = Ember.Route.extend({
model: function() {
// preload all data from the server once
this.store.find('person');
}
});
App.LinksRoute = Ember.Route.extend({
model: function() {
// get the local data without request the server
return this.store.all('person');
}
});
App.OtherRoute = Ember.Route.extend({
model: function() {
// get the local data without request the server
return this.store.all('person');
}
});
I made a fiddle with this please give a look http://jsfiddle.net/marciojunior/Az2Uc/ 我做了一个小提琴,请看看http://jsfiddle.net/marciojunior/Az2Uc/
That fiddle uses the jquery mockjax, if you see the browser console the MOCK GET: /people
is showed just once, this is like a regular xhr request, but it's mocked. 那个小提琴使用jquery mockjax,如果你看到浏览器控制台
MOCK GET: /people
只显示一次,这就像一个常规的xhr请求,但是它被嘲笑了。 Transitioning to people1
and people2
won't perform other requests just get the local data. 转换到
people1
和people2
将不会执行其他请求只获取本地数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.