find 方法将始终返回一个承诺,该承诺将与记录一起解决。如果记录已经在 store 中,promise 将立即解决。否则,store 会询问适配器的 find 方法来查找必要的数据。
这仅在通过 id 查找时有效this.store.find('link', 1)。Usingthis.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');
}
});
我对此做了一个小提琴,请看一下http://jsfiddle.net/marciojunior/Az2Uc/
那个小提琴使用 jquery mockjax,如果你看到浏览器控制台MOCK GET: /people只显示一次,这就像一个常规的 xhr 请求,但它是模拟的。转换到people1并且people2不会执行其他请求只是获取本地数据。