This in template helpers

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

I have a following document: meeting

{"matches":["J6RbxgZaB6thX3Db5","QzdkaAJkBywwAa6rA","8e939ebZ9vmr9vbo5","r6ExKfumkytAdwPHs"],"start_time":"2015-08-07T09:24:27.466Z","_id":"6qfcj3fo6yvbnd9y7"}

templates

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

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

Helpers

Template.session.helpers
	meeting: ->
		Meeting.findOne FlowRouter.getParam 'sessionId'

Template.scorerow.helpers
	'a_front': ->
		log this
		matchupId=JSON.stringify(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"}
utils.coffee:1 "r6ExKfumkytAdwPHs"
utils.coffee:1 Matchup: []
utils.coffee:1 "r6ExKfumkytAdwPHs": []

However, after typing on the console:

>>Matchup.findOne("r6ExKfumkytAdwPHs")
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?

thanks

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.

1 Like

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

Template.scorerow.helpers
    '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:
https://www.discovermeteor.com/blog/spacebars-secrets-exploring-meteor-new-templating-engine/

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

Template.helpers({
    meeting: ->
        this.meeting.matches
})

// does not equal to

Template.events({
    'click #stuff': ->
        this.meeting.matches
})