offirgolan / ember-parachute

Improved Query Params for Ember

Home Page:https://offirgolan.github.io/ember-parachute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reload vs refreshModel

juggy opened this issue · comments

Hello,

I was under the impression that the reload option was to replace the refreshModel on the route, but it seems it does not.

It would be nice to actually regroup the route query params options under the same parachute config within the controller: https://emberjs.com/api/ember/2.14/classes/Ember.Route/properties/queryParams?anchor=queryParams

@juggy I'm not sure I follow you here.

On the Route there is a queryParams property you can set to set some options like refreshModel (https://emberjs.com/api/ember/2.14/classes/Ember.Route/properties/queryParams?anchor=queryParams).

What I was saying is: It would be nice to have the config you do with ember-parachute be applied to route as well as the controller.

@juggy ember-parachute moves all the query param handling to the controller, and you have to manage your data fetching in the queryParamsDidChange controller hook. The route loses all information it has about the query params; it doesn't need it anymore.

I'm using a construct like this to refresh the model when certain query params change:

const myQueryParams = new QueryParams({
  foo: {
    refresh: true // N.B. it's `refresh', not `reload'
  },
  bar: {
    refresh: false
  }
});

export default Ember.Controller.extend(myQueryParams.Mixin, {
  queryParamsDidChange({ shouldRefresh, queryParams }) {
    if (shouldRefresh) {
      // this will only fire if the `foo' query param changed
      this.transitionToRoute({ queryParams }); // or replaceRoute
    }
  }
});

This lets you preserve your existing model hooks and loading behavior while leveraging ember-parachute.

@mwpastore controller#transitionToRoute does not seem to trigger the model hook for me - any ideas? queryParamsDidChange is definitely getting triggered, and shouldRefresh is definitely true, but I am not seeing model fire again. Any thoughts?

This does not seem possible to me - you cannot refresh the route with new QPs from the same route using transitionToRoute.

The alternative seems to be to use a route action so you have access to this.refresh, but if you have to do this in a child route, it seems like you'll get "unhandled action" errors when navigating from a parent route:

// routes/application.js
@action
refreshModel() {
   this.refresh();
}

// controllers/show-geography.js
...
  queryParamsDidChange({ shouldRefresh }) {
    if (shouldRefresh) {
      // this will only fire if the `foo' query param changed
      // this.send('refreshModel');
      this.send('refreshModel');
    }
  }
...

My problem with this approach is that it's unclear which model will be getting refreshed. If I have an application.js model, will only that be refreshed?