ember-cli-pagination icon indicating copy to clipboard operation
ember-cli-pagination copied to clipboard

How to not reload model everytime route changes

Open eguneys opened this issue 10 years ago • 6 comments

Whenever I change a page, the request is made, even if I browse back to route that the model is already loaded for. Is there a way to not request already loaded model? I also have an extra query parameter tab and it also reloads the model whenever it changes, can I make it so that it loads the model once?

// Route
export default Ember.Route.extend(RouteMixin, {
  queryParams: {
    tab: {
      refreshModel: true
    }
  },
  model: function(params) {
    params.paramMapping = {
      perPage: "size"
    };
    return this.findPaged('article', params);
  }
});

Here's a sample network output:

articles?page=4&size=10&tab=active
articles?page=5&size=10&tab=active
articles?page=4&size=10&tab=active

See page=4 is loaded twice.

eguneys avatar Dec 09 '14 17:12 eguneys

Is refreshModel in this chunk actually currently doing something, or is this an example of how the feature could potentially work?

queryParams: {
    tab: {
      refreshModel: true
    }
  },

mharris717 avatar Dec 09 '14 18:12 mharris717

refreshModel is what causes a fullTransition thus the model reload, and that's just for the tab query param, nothing to do with pagination. See http://emberjs.com/guides/routing/query-params/#toc_opting-into-a-full-transition.

If I don't set refreshModel, model never reloads, and when I use it it always reloads, but I want it to reload once.

I don't know how pagination uses the query params, but I want reload once behaviour with the pagination as well.

eguneys avatar Dec 09 '14 18:12 eguneys

Gotcha. Always love to learn something.

I will take a look. My initial outlook isn't optimistic, but I'm often wrong.

Just to clarify, your sample output is for when you displayed page 4, clicked 5, then clicked 4 again, correct?

mharris717 avatar Dec 09 '14 19:12 mharris717

What do you mean not optimistic, you think it's not possible? It happens when I click page 4, page 5, page 4. So whenever you click a link it reloads the model. I thought ember-data doesn't reload if it is already in the cache, maybe it's related to ember-data? Because same thing happens with the query params. When I opt in to full transition, it always reloads.

eguneys avatar Dec 09 '14 19:12 eguneys

It's certainly possible, just thinking about how easy/advisable it would be, based on what I (think I) know about the Ember Data store.

PagedRemoteArray makes a call to store.find, with params, every time the page changes.

What follows is how I believe things work, but I haven't tested it. Apologies if I am saying things you already know.

Ember Data won't do a new remote request if you do a vanilla find all a 2nd time

this.store.find('article')

// ...

// will not make a 2nd remote request
this.store.find('article')

However, calling find with params is different. Ember Data, at least with default configuration, will make a new request every time

this.store.find('article', {page: 2})

// ...

// will make a 2nd remote request
this.store.find('article', {page: 2})

This makes sense, if you think about examples like this

this.store.find('article',{latest: true})

If the Ember Data store cached the results of a query with params, this issue would be resolved.

We could add some kind of caching layer in PagedRemoteArray, but my initial thought is that I am strongly opposed to that. That's Ember Data's job, and duplicating it will lead to bugs.

There absolutely could be a way to make "cached find with params" calls to the store. I'd be happy to add support for that, if it exists. If this is happening because of some kind of bug in this library, I would obviously love to fix that as well.

mharris717 avatar Dec 09 '14 19:12 mharris717

I have noticed two of the same requests going off l after I switch the page on the pagination which seems odd, I need to dig into it more, to figure out what is making store.find get triggered twice on pagination change, could be specific to my app or a bigger problem

jcope2013 avatar Dec 11 '14 01:12 jcope2013