Zack Moore
Zack Moore

Reputation: 41

Reload Ember Parent Model on Child Route Change

I have a parent route that holds a series of tabs (which are child routes).

The parent route has a model that hits a JSONAPI endpoint for gathering data that is tangentially related to the data of its children.

The data that the parent route accesses can change fairly quickly, so I would like to update it whenever the user traverses between the child routes.

Is there a way to essentially re-resolve a model hook on a parent route each time that one of its children is transitioned to?

Have tried something similar to below, but reload doesn't seem to be a method available to the parent model.

Parent route

export default Route.extend({
  model() {
    return this.store.findAll('parentData');
  }  
});

Child route

export default Route.extend({
  beforeModel() {
    this.modelFor('parentRoute').reload();
  }
});

Upvotes: 3

Views: 4094

Answers (3)

Jennie Ji
Jennie Ji

Reputation: 809

I recommend to use 'willTransition' hook on parent route. Every children route transition will trigger parent route's willTransition hook, and it won't be fired when the parent route just initialize.

So it will be look like this:

export default Route.extend({
    model() {
        return this.store.findAll('parentData');
    },
    actions: {
        willTransition() {
            this.controller.get('model').reload();
        }
    }
});

Upvotes: 1

Igor
Igor

Reputation: 1588

If you reload the parent model, the child route model will get reloaded as well, leading you into infinite loop of reloading.

Think the best way for you would be just to do findAll() on your child routes, and use that data instead of the parent route model.

Upvotes: 1

Jovica Šuša
Jovica Šuša

Reputation: 622

You can reload your parent route model using yourParentModel.reload() when you transition to child routes.

Upvotes: 0

Related Questions