如何在流星模板中创建全局函数

Ram*_*san 11 meteor meteor-blaze meteor-helper

如何为流星中的所有模板创建一个函数?

index.js

// Some function
function somefunction(){
  return true;
}
Run Code Online (Sandbox Code Playgroud)

Test1.js

Template.Test1.events({
  'click button' : function (event, template){
    //call somefunction
  }
});
Run Code Online (Sandbox Code Playgroud)

Test2.js

Template.Test2.events({
  'click button' : function (event, template){
    //call some function
  }
});
Run Code Online (Sandbox Code Playgroud)

sai*_*unt 21

您需要使您的函数成为全局标识符,以便能够跨多个文件调用它:

index.js

// Some function
somefunction = function(){
  return true;
};
Run Code Online (Sandbox Code Playgroud)

在Meteor中,默认情况下变量是文件范围的,如果要将标识符导出到全局命名空间以在项目中重用它们,则需要使用以下语法:

myVar = "myValue";
Run Code Online (Sandbox Code Playgroud)

在JS中,函数是可以存储在常规变量中的文字,因此具有以下语法:

myFunc = function(){...};
Run Code Online (Sandbox Code Playgroud)