Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor/Iron路由器-如何将值从RouteControl传递给模板助手_Meteor - Fatal编程技术网

Meteor/Iron路由器-如何将值从RouteControl传递给模板助手

Meteor/Iron路由器-如何将值从RouteControl传递给模板助手,meteor,Meteor,如果我有如下路线控制器: add_departmentController = RouteController.extend({ before: function(){ var a = this.params._id; var b = 'abc'; } }); 如何在模板的帮助器中访问这些值 Template.add_department.helpers({ SomeProperty: function () { //H

如果我有如下路线控制器:

add_departmentController = RouteController.extend({
    before: function(){
        var a = this.params._id;
        var b = 'abc'; 
    }
});
如何在模板的帮助器中访问这些值

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
    },
});

使用控制器中的数据功能

add_departmentController = RouteController.extend({
    template: 'departmentTemplate',
    data: function(){
        return {_id: this.params._id};
    }
});
这会将返回的数据函数对象作为数据上下文“注入”到模板中

[编辑]

模板: {{{u id}}直接来自数据上下文,{{idFromHelper}}从模板助手函数返回{u id}

<template name="departmentTemplate">

  {{_id}}

  {{idFromHelper}}

</template>

您可以使用客户端代码中的
Router
对象访问当前控制器:

Template.addDepartment.helpers({
  someHelper: function() {
    var controller = Router.current();
    // return the _id parameter or whatever
    return controller.params._id;
  }
});

我知道控制器中的数据函数,但我不知道如何在helper函数或模板中访问它-您可能有一个如何在这两个函数中使用/访问数据对象的示例吗?嘿@A_L我编辑了答案,希望有帮助。这应该很容易,因为数据就在Handlebar模板和模板对象本身的数据上下文中。因此,您应该能够通过“this.\u id”引用它。如果这不适用于您(但应该适用),您希望检查是否可以通过“this.data.\u id”引用它。layoutTemplate是否也可以访问相同的数据功能?
Template.addDepartment.helpers({
  someHelper: function() {
    var controller = Router.current();
    // return the _id parameter or whatever
    return controller.params._id;
  }
});