0
votes

I read this online in a tutorial:

Because we supplied a model to the link (book.author), Ember is not calling the model() hook and therefore no question arises about reloading data. Our fix is simple enough: pass in book.author.id so that model() gets called.

I am trying to overwrite the shouldBackgroundReloadRecord hook in an author.js adapter, which I think only gets called when the model() hook gets called with findRecord... but I never see the console.log message which makes me think the model hook isn't being called in the route handler:

This method is used by the store to determine if the store should reload a record after the store.findRecord method resolves a cached record.

This method is only checked by the store when the store is returning a cached record.

If this method returns true the store will re-fetch a record from the adapter.

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({

  shouldReloadRecord(store, snapshot) {
    return false;
  },

  shouldBackgroundReloadRecord(store, snapshot) {
    console.log("Calling shouldBackgroundReloadRecord");
    const loadedAt = snapshot.record.get('loadedAt');

    // if it was loaded more than an hour ago
    if (Date.now() - loadedAt > 3600000) {
      return true;
    } else {
      return false;
    }
  }

});

This is my templates/books.hbs with the link-to:

{{!-- app/templates/books.hbs --}}
...
{{#link-to 'author' book.author.id class="author" bubbles=false }}{{book.author.name}}{{/link-to}}
...

To my understanding, anytime we click a link-to, we hit the router.js, which then determines what route-handler gets called. When a route-handler gets called, doesn't the model() hook get called in the route handler?

My route handler for author is pretty normal and pretty irrelevant probably, but here it is.

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.findRecord('author', params.author_id);
  }
});
2

2 Answers

2
votes

I suggest you consult the Ember.Route#model documentation:

Note that for routes with dynamic segments, this hook is not always executed. If the route is entered through a transition (e.g. when using the link-to Handlebars helper or the transitionTo method of routes), and a model context is already provided this hook is not called.

A model context does not include a primitive string or number, which does cause the model hook to be called.

Followed by code samples and whether they trigger or not the model hook.

1
votes

When you change query params through a link-to, it is not considered a full transition. This means that no Route method hook like model will be called. If you need a query param change to trigger a full transition, and thus the method hooks, you can use the optional queryParams configuration hash on the Route. If you have a authoerId query param and you want it to trigger a model refresh, you can set it as

//Route

import Route from '@ember/routing/route';

export default Route.extend({
  queryParams: {
    authorId: {
      refreshModel: true
    }
  },

  model({ authorId }) {
    return this.store.findRecord('author', authorId);
  }
});

// Controller

import Controller from '@ember/controller';

export default Controller.extend({
  queryParams: ['authorId'],
  authorId: null
});