Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor/Iron-Router - How pass values from RouteController to template helper

Tags:

meteor

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
    },
});
like image 751
A_L Avatar asked Dec 19 '13 09:12

A_L


2 Answers

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;
    }
})
like image 200
DerMambo Avatar answered Nov 11 '22 09:11

DerMambo


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;
  }
});
like image 13
Loqman Avatar answered Nov 11 '22 11:11

Loqman