lundi 30 janvier 2017

Ember.Route - Model hook is called twice instead of one

I would like to redirect an incomplete URL to a complete correct URL:

http://localhost/product/12/a-single-pr -> http://localhost/product/12/a-single-product-name

The problem is that the model hook is called twice instead of one, making two identical requests to retrieve a single object. Any clues?

routes/product.js

import Ember from 'ember';

export default Ember.Route.extend({
  afterModel(model, transition) {
    let formatted = model.get('formatted');

    if (transition.params.product.formatted !== formatted) {
      let path = '/product/' + model.id + '/' + formatted;
      this.replaceWith(path, model);
    }
  },
  model(params) {
    return this.get('store').findRecord('product', params.product_id);
  }
});

router.js

...

Router.map(function() {
  this.route('product', {path: '/product/:product_id/*formatted'});
});

...




Aucun commentaire:

Enregistrer un commentaire