ember-data - store.find('model') 总是查询服务器 [英] ember-data - store.find('model') always queries the server

查看:14
本文介绍了ember-data - store.find('model') 总是查询服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我可能误解了 store.find() 方法的工作原理,但是,根据我的理解,如果我要求的记录已经存在,以下代码不应查询服务器商店:

var IndexRoute = Em.Route.extend({模型:函数(){return this.store.find('link');},});

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

<块引用>

find 方法将始终返回一个承诺,该承诺将与记录一起解决.如果该记录已经在商店中,则承诺将立即得到解决.否则,商店将要求适配器的 find 方法找到必要的数据.

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

解决方案

find 方法总是会返回一个会被解析的承诺与记录.如果记录已经在商店中,则承诺将立即解决.否则店家会问适配器的 find 方法来查找必要的数据.

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

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

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

App.ApplicationRoute = Ember.Route.extend({模型:函数(){//一次从服务器预加载所有数据this.store.find('人');}});App.LinksRoute = Ember.Route.extend({模型:函数(){//获取本地数据,无需请求服务器return this.store.all('person');}});App.OtherRoute = Ember.Route.extend({模型:函数(){//获取本地数据,无需请求服务器return this.store.all('person');}});

我对此做了一个小提琴,请看一下http://jsfiddle.net/marciojunior/Az2Uc/>

那个小提琴使用 jquery mockjax,如果你看到浏览器控制台 MOCK GET:/people 只显示一次,这就像一个常规的 xhr 请求,但它被嘲笑.转换到 people1people2 不会执行其他请求,只会获取本地数据.

Details: ember-data-1.0.0.beta.3 and the default 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:

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

From the emberjs.com documentation for DS.Store.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.

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. Shouldn't .find() handle this?

解决方案

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.

This just work when finding by id this.store.find('link', 1). Using this.store.find('link') will always perform requests in the server.

You can get the local data using the all method this.store.all('link'). But in some place of your app, you will need to preload that data using the find method. Otherwise all will return nothing.

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/

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. Transitioning to people1 and people2 won't perform other requests just get the local data.

这篇关于ember-data - store.find('model') 总是查询服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆