Search code examples
javascriptnode.jsiron-routermeteor

Replace the %20 that means a space on iron router


I'm trying to replace the %20 that means space on the url by another character,

Router.map(function () {
  this.route('promos', {
    path: '/:promo_name',
    waitOn: function(){
        return Meteor.subscribe("promo", this.params.promo_name);
    },
    data: function(){
      return Promociones.findOne({'metadata.nombrePromo': this.params.promo_name});
    }
  });
});

This is how I generate dynamic routes, and i get something like this http://padonde.mx/Pi%C3%B1a%20Colada%202x1 i want to replace the %20 by another character like - or +, is this possible on iron router?


Solution

  • This is actually an stander URL encoding, and its set by the browser not iron-router.

    You can do this,

    Where you insert the

    'metadata.nombrePromo', add another field to the collection like
    

    'metadata.nombrePromoReplace', and do this. i guess you have something like

    var nombrePromo = $('#idElementPromo').val();, or a session or whatever you pass the value to the metadata

    so based on that var nombrePromo do this.

    var nombrePromoReplace = nombrePromo.replace(/\s+/g, '');
    

    and now change the route.

    Router.map(function () {
      this.route('promos', {
        path: '/:promo_name',
        waitOn: function(){
            return Meteor.subscribe("promo", this.params.promo_name);
        },
        data: function(){
          return Promociones.findOne({'metadata.nombrePromoReplace': this.params.promo_name});
        }
      });
    });
    

    Now when you navigate to /:promo_name, and if you added something like

    i have blanks spaces
    

    the route should be

    /ihaveblankspaces
    

    It should work.