dev@cloudburo

Meteor Small Hints #4: Reading this.params values in the iron-router Data Context

Iron-router is a powerful Meteor router that works on the server and browser.

In this article we describe how you can read out a query parameter via the data context of the iron-router, which then can be processed later in the template. In the below example we have outsourced the routing logic for /dashboard to an iron-router controller file.


  Router.route '/dashboard', name: "dashboard", controller: 'DashboardController' 


In the URL call we will pass in a query parameter, e.g. https://bots.cloudburo.net/dashboard?regCode=“T@myProductName

Now in order to retrieve this query parameter - via the this.params object - in the iron-router data context, you have to use for the data assignment a function . Trying it without a function call the this.params will not be returned. This took m quite some time to figure out, I always tried it with direct assignment in the data section.


  exports.DashboardController = AppController.extend 


    waitOn: () -> 
      this.subscribe 'userData' 
      this.subscribe 'productTypes' 
      this.subscribe 'products' 
      this.subscribe 'productAdmins' 


    # Use a function to allocate the data context, when you require this.params values 
    data: () -> 
      if this.params? && this.params.query? && this.params.query.regCode? 
        Plattform.log("Dashboard Controller called with "+this.params.query.regCode) 
        regC = this.params.query.regCode 
      else 
        regC = undefined 
      templateData = 
        productTypes: ProductTypes.find() 
        productAdmins: ProductAdmins.find() 
        currencySymbol: Plattform.getCurrencySymbol() 
        products: Products.find({}) 
        regCode : regC 
      return templateData 


In the template itself I can reference directly the regCode value


  Template.dashboard.rendered = () -> 
      Plattform.debug "Template.dashboard.rendered" 
      $('.statusicon').tooltip(); 
      if this.data.regCode? 
        if Products.find().fetch().length == 0 
          Plattform.log("A new client from subscriber page - regCode: "+this.data.regCode) 
          splits = this.data.regCode.split("@") 
          if splits.length  == 2 
            productType = ProductTypes.findOne(name: splits[1]) 
            if productType? 
              if splits[0] == "T" 
                testProduct productType._id 
              else if splits[0] == "S" 
                buyProduct productType._id 


This blog entry was fully produced within Evernote and published using the Cloudburo Publishing Bot.

comments powered by Disqus