samedi 5 mars 2016

When does the model() hook in the route handler get called anyway? Does it get called in a link-to in Ember.js?

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);
  }
});




Aucun commentaire:

Enregistrer un commentaire