简体   繁体   English

这个代码块在 javascript 语法中是什么意思?

[英]What does this code block mean in javascript syntax?

Template.body.helpers({
    tasks() {
      return Tasks.find({});
    },
});

I just know the basics of javaScript..but I saw this piece of code in meteor tutorial and did not understand what it meant,is task an object or a function?How does this actually give info to the HTML page? I just know the basics of javaScript..but I saw this piece of code in meteor tutorial and did not understand what it meant,is task an object or a function?How does this actually give info to the HTML page?

Actually, the Template.body.helpers is a dictionary which acts very similar to an object , so you can set multiple properties in it, like functions.实际上, Template.body.helpers是一个与object非常相似的字典,因此您可以在其中设置多个属性,例如函数。 The tasks() itself is a function which is responsible returning data from our database (We know the Tasks are like this const Tasks = new Mongo.Collection('tasks'); so it is connected to the tasks table) , in this particular case our query ( find({}) ) does not include any condition so it will return all the data from the tasks table. tasks()本身是一个 function 负责从我们的数据库返回数据(我们知道任务就像这样const Tasks = new Mongo.Collection('tasks');所以它连接到tasks表) ,在这个特定的如果我们的查询( find({}) )不包含任何条件,因此它将返回tasks表中的所有数据。 The meteor engine will take the rest of it where you can refer to your helpers by {{tasks}} . meteor 引擎将采用 rest ,您可以通过{{tasks}}引用您的助手。

So you can create as many helpers as you want to do what you desired just like this:因此,您可以创建任意数量的助手来做您想做的事情,就像这样:

Template.body.helpers({
    tasks() {
      return Tasks.find({});
    },
    foo() {
      return Session.get("foo");
    }
});

Then you can refer both foo and tasks in your HTML like before with {{foo}} and {{tasks}} .然后,您可以像以前一样使用{{foo}}{{tasks}}来引用 HTML 中的footasks

It is the function, to which you are passing an object as an argument.它是 function,您将 object 作为参数传递给它。 In this object there is method called "tasks".在这个 object 中,有一个称为“任务”的方法。


tasks() {
      return Tasks.find({});
}

is the same as是相同的

tasks: function(){
    return Tasks.find({});
)

It is ES6 syntax.它是 ES6 语法。

It's ES6+.它是 ES6+。

Template.body.helpers is receiving a JS Object/JSON which has a function called tasks . Template.body.helpers正在接收一个 JS 对象/JSON,它有一个名为tasksfunction And it get all the information from Tasks object (defined anywhere in the same code), with empty query (which'd like all).它从Tasks object (在同一代码中的任何位置定义)获取所有信息,并带有空查询(全部都需要)。

Template.body.helpers({
  tasks: function () {
    return Tasks.find({});
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM