最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ember-data - store.find('model') always queries the server - Stack Overflow

programmeradmin12浏览0评论

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 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?

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?

Share Improve this question edited Dec 24, 2016 at 14:00 Marcio Junior 19.1k4 gold badges46 silver badges47 bronze badges asked Oct 29, 2013 at 10:08 Jorge MarquesJorge Marques 6168 silver badges17 bronze badges 1
  • Do you have the Ember plugin for Chrome? You can check to see if the data is in the Ember store. – claptimes Commented Oct 29, 2013 at 12:00
Add a comment  | 

1 Answer 1

Reset to default 17

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.

发布评论

评论列表(0)

  1. 暂无评论