Search code examples
javascriptember.jsmetadatalink-toserialization

ember normalizeResponse when navigated to page from link-to


When I navigate to a specific page, the overridden function normalizeResponse in my serializer used in conjunction with code in my router model function, to add meta data to my model, works correctly. Basically, normalizeResponse runs first, then my model function in my router.

serializers/application.js

import App from '../app';
import JSONAPISerializer from 'ember-data/serializers/json-api';

App.storeMeta = {};

export default JSONAPISerializer.extend({
  normalizeResponse(store, primaryModelClass, payload){
    App.storeMeta[primaryModelClass.modelName] = payload.meta;
    return this._super(...arguments);
  }
});

And in my model.

import App from '../app'
...
model(params){
  const data = {};

  return this.store.findRecord('myModelType', params.id).then((myModelType)=>{
    myModelType.meta = App.storeMeta['myModelType'];
    return myModelType;
  },()=>{ //error
    this.get('session').invalidate();
  });
}

When I navigate to that specific page through a link-to from another page, the model code gets called first, so there is no meta data being attached to the model.

How do I get the normalizeResponse function to run before the model function when navigated to from link-to?

Any help would greatly be appreciated.


Solution

  • The answer for anyone who sees this is to add {reload: true} as a param to the findRecord function.

    So the second code snippet from my original post would know look like the following:

    import App from '../app'
    ...
    model(params){
      const data = {};
    
      return this.store.findRecord('myModelType', params.id, {reload: true}).then((myModelType)=>{
        myModelType.meta = App.storeMeta['myModelType'];
        return myModelType;
      },()=>{ //error
        this.get('session').invalidate();
      });
    }
    

    More info here. Thanks to that site for the answer.