gpt4 book ai didi

javascript - ember-data - store.find ('model' ) 总是查询服务器

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:24 26 4
gpt4 key购买 nike

详细信息:ember-data-1.0.0.beta.3 和默认的 RESTAdapter

我可能误解了 store.find() 方法的工作原理,但是,据我了解,如果我要查询的记录已经存在于商店:

var IndexRoute = Em.Route.extend({
model: function() {
return this.store.find('link');
},
});

来自 DS.Store.find() 的 emberjs.com 文档:

The find method will always return a promise that will be resolved with the record. 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.

我有另一条路线具有完全相同的模型 Hook ,但是当我访问该路线时,即使数据已经在商店中,服务器也会被查询。如果我回到 Index 路由,它会再次被查询。 .find() 不应该处理这个吗?

最佳答案

The find method will always return a promise that will be resolved with the record. 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.

这只在通过 id this.store.find('link', 1) 查找时起作用。使用 this.store.find('link') 将始终在服务器中执行请求。

您可以使用all 方法this.store.all('link') 获取本地数据。但是在您应用的某些地方,您需要使用 find 方法预加载该数据。否则 all 将不返回任何内容。

您可以使用以下方法获得所需的行为:

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

我做了一个 fiddle 请看看http://jsfiddle.net/marciojunior/Az2Uc/

那个 fiddle 使用 jquery mockjax,如果你看到浏览器控制台 MOCK GET:/people 只显示一次,这就像一个常规的 xhr 请求,但它被模拟了。过渡到 people1people2 不会执行其他请求,只会获取本地数据。

关于javascript - ember-data - store.find ('model' ) 总是查询服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com