This in template helpers


I am a bit confused about what this should be in a template helper.

I have a following document: meeting



<template name="session">
		<th>A Front</th>
		<th>B Front</th>
	{{#each meeting.matches}}
		{{> scorerow }}

<template name="scorerow">
		<td><input value="{{a_front}}"/></td>
		<td><input value="{{b_front}}" /></td>
		<td><input size="2" /> &ndash; <input size="2" /></td>


	meeting: ->
		Meeting.findOne FlowRouter.getParam 'sessionId'

	'a_front': ->
		log this
		log matchupId
		res = Matchup.find({_id:matchupId}).fetch()
		log "Matchup: %o", res
		return 'boo' unless res
		log "%s: %o", matchupId, res
		return res.a_front

And I get the following log (treat log as an alias for console.log)

String {0: "r", 1: "6", 2: "E", 3: "x", 4: "K", 5: "f", 6: "u", 7: "m", 8: "k", 9: "y", 10: "t", 11: "A", 12: "d", 13: "w", 14: "P", 15: "H", 16: "s", length: 17, [[PrimitiveValue]]: "r6ExKfumkytAdwPHs"} "r6ExKfumkytAdwPHs" Matchup: [] "r6ExKfumkytAdwPHs": []

However, after typing on the console:

Object {_id: "r6ExKfumkytAdwPHs", a_fwd: "8FtzATa2J93qxRW5C", a_bck: "4vQYoRpGZ9PLrvD5r", b_fwd: "H7skjaLr84Pvi396A", b_bck: "5KZsuKvC6zehPPLCP"…}

What could be the problem, and how to iterate over the Matchups?


ps using coffeescript and FlowRouter, if matters

Ok, found one typo, JSON.stringify will have quotes, that’s why it doesn’t work.


Your each meeting.matches iterates over the array of strings, and within in it this will refer to your id, however, you’re not passing it into your nested template (scorerow)
you need to pass it in to use it like
{{> scorerow matchId=this}}

and then in scorerow, I think you can reference it with this.matchId or something along those lines, search up passing variables into templates.


Strangely, if you don’t pass anything to scorerow, this becomes something very similar to matchid in the scorerow helpers. What is that object?

    'match': ->
        log "This is %o", this

results in

This is  String [ "J", "6", "R", "b", "x", "g", "Z", "a", "B", "6", még 7… ]


So should I always pass this as argument? Isn’t there something I can rely (this) to pass data to subtemplates?


Not always because you don’t always need a parent state in a child template. But where you do and it’s not in a route, then you can pass it in as a parameter. Have a read of this, it explains it alot better than I ever could :stuck_out_tongue:

Sessions, Minimongo, globals are the only things you can access from anywhere. this changes when your in a template to when your in a helper to an event to any hooks etc… you cannot rely on this to be your arbitrary ‘parent’ template’s data context. How does the application know what you want ‘this’ to be? Rather, this is whatever the execution context of javascript is in at that point in time.
this in your template is not the same as this in your helper. i.e:

{{> each meeting.matches}} // -> same as this.meeting.matches

// does not equal to

    meeting: ->

// does not equal to{
    'click #stuff': ->