0

with meteor's IronRouter, I'm trying to use the this.params object elsewhere, but confused as to what it is. It seems to be a zero length array, that is actually an object with named methods after the path components.

# coffee
@route 'magnets',
  path: '/magnets/lesson/:lessonCname'

  data: ->
    if @ready()
      debugger;
      console.log("route.params", @params)

with this code, in the debug console I will get:

this.params
[]
this.params.lessonCname
"despite-magnets-01"
typeof(this.params)
"object"
this.params.length
0
this.ready()

but in passing the params object to a server method, the methods (ie "lessonCname") disappear.

If my understanding is correct, then the near-term question is what is the best way to retrieve/convert these methods to {property:value} so they can be serialized and passed to server calls?

dcsan
  • 11,333
  • 15
  • 77
  • 118

3 Answers3

0

There are two easy ways of solving your problem, you can either set a global variable from within the data scope (but this is considered bad practice, at least IMO) or you can use the "data" function, which returns the data context for the current template:

  data: ->
        window._globalscopedata = @params.whatever        #setting global variable
        return someCollection.findOne                     #returns data context
            _id: @params.whatever 

when proccessing this route I will have the whatever param available in _globalscoredata and my document available in the template context.

Erez Hochman
  • 1,588
  • 1
  • 11
  • 14
0

The params property attached to a RouteController is an object with the following properties :

  • hash : the value of the URL hash.
  • query : an object consisting of key/value pairs representing the query string.
  • a list of URL fragments with their name and actual value.

Let's take an example, for this route definition :

// using iron:router@1.0.0-pre2 new route definition
Router.route("/posts/:slug");

And this URL typed in the browser address bar : /posts/first-post#comments?lang=en

We can use the console to find out precisely what params will actually contain :

> Router.current().params

Which will display this result :

Object {
  hash: "comments",
  slug: "first-post",
  query: {
    lang: "en"
  }
}

Here slug is already a property of the params object whose value is "first-post", this is not a method.

If you want to extract from params these URL fragments as an object of key/value pairs, you can use underscore omit :

// getting rid of the hash and the query string
var parameters=_.omit(this.params,["hash","query"]);
saimeunt
  • 22,666
  • 2
  • 56
  • 61
0

Take a look at the source code for retrieving the parameters from a path. params is an array, but may have named properties. To iterate over everything, you can use the for in loop:

for(var x in myArray){
    // Do something.
}

In this way, you can copy over everything to a new object (there may be a simpler way to create a copy).

Peppe L-G
  • 7,351
  • 2
  • 25
  • 50