If I have a route controller as follows:
add_departmentController = RouteController.extend({
before: function(){
var a = this.params._id;
var b = 'abc';
}
});
How can I access these values in a helper for the template
Template.add_department.helpers({
SomeProperty: function () {
//Here I need access to a or b from above
//Would also be nice to access 'this' directly eg. this.params
},
});
Use the data function in the controller
add_departmentController = RouteController.extend({
template: 'departmentTemplate',
data: function(){
return {_id: this.params._id};
}
});
This "injects" the returned object of the data function as the data-context into your template.
[EDIT]
Template: The {{_id}} comes directly from the data context, {{idFromHelper}} returns the _id from a template helper function.
<template name="departmentTemplate">
{{_id}}
{{idFromHelper}}
</template>
Helper:
Template.addDepartment.helpers({
idFromHelper: function() {
return this._id;
}
})
You can use Router
object inside your client code to access the current controller:
Template.addDepartment.helpers({
someHelper: function() {
var controller = Router.current();
// return the _id parameter or whatever
return controller.params._id;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With