I might be overlooking it, but I can’t seem to find how to set the data scope/context for templates & routes… ?
If I want to load a document to be used as the context for a template, I have no idea how to do it unless I use a helper and wrap the template with {{with}}…
Yeah so thinking about this some more, there are many more advantages to having a data context for the template.
You can retrieve the context through helpers and events etc…
Isn’t that completely missing now?
I would have thought he’s missing Iron Router, isn’t he? (given the topic name)
I think using {{#with templateHelper}} is actually the way to go if you’re going to be using a lot of pieces of info from the data context (in this case, a single document from the Articles collection). Using a helper like
Template.myTemplate.helpers({
article: function () {
return Articles.findOne({some:"condition"})
}
});
is okay if you’re happy to do a findOne every time you want something from the data context in your template, but if the Articles collection has a large number of documents on the client and you’re doing findOne repeatedly to supply your template with little bits of data, this can lead to slow down in rendering. Using with you’re just doing the findOne once.
If you’re just subscribed to the one document from the Articles collection, then nothing to worry about – either approach will be nice and snappy.
Sorry my post could have been 100000x clearer if I explained myself better!
With iron router you can set the data context for a route using:
Router.route('/post/:_id', function () {
this.render('Post', {
data: function () {
return Posts.findOne({_id: this.params._id});
}
});
});
Which is nice and simple.
With flow router it doesn’t have that same shortcut…
So templates themselves don’t get their data context set by the router, you need to wrap them with {{#with datacontext}} before you insert the template.
Yeah the only thing that is a bit annoying about that is, then you need to have two templates… One for the router to reference, and then you need to nest another within the {{#with}} if you want to have the context recognised against the template as a whole…
I could be missing something simple, I’ve been using iron-router since day 1 and I am sure I have just not learned the proper way to do some things…
example:
// this is the one, which your router points to...
<template name="outertemplate">
{{#with dataContext}}
{{> innerTemplate}}
{{/with}}
</template>
<template name="innertemplate">
This template can make use of the data context...
As can all the helpers, events and functions for `innertemplate` ...
</template>
Obviously you are troubled, but I still cannot see the reason. Taking your code above, let’s put it this way:
// this is the one, which your router points to...
<template name="outertemplate">
{{> innerTemplate}}
</template>
<template name="innertemplate">
{{#with dataContext}}
...
{{/with}}
</template>
And finally this way:
// this is the one, which your router points to...
<template name="mytemplate">
{{#with dataContext}}
...
{{/with}}
</template>
For Template.mytempate.onCreated, no this.data won’t be the data context you want. But for the helpers and events, presumably they’re all going to be used/triggered by elements inside the #with block, so this will be the data context you’re after.