Meteor - 什么是 Spacebars.kw {hash: Object} [英] Meteor - What is Spacebars.kw {hash: Object}

查看:50
本文介绍了Meteor - 什么是 Spacebars.kw {hash: Object}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个可以放置在模板中的 Meteor 包.所以我首先尝试注册一个助手.

I'm attempting to write a Meteor package which can be placed inside templates. So I first attempted to register a helper.

Template.registerHelper('testHelper', function(a, b) {
        console.log(a);
        console.log(b);
})

我已经在 /packages 中添加了包,在我的客户端模板中,当我添加 {{testHelper "hello" "meow"}} 时,控制台记录了 hellomeow,这正是我所期望的.

I've added the package inside /packages, and in my client template, when I added {{testHelper "hello" "meow"}}, the console logged hello and meow, which is what I expected.

当我添加 {{testHelper "hello"}} 时,我希望控制台记录 hellonull,因为没有传递任何内容作为第二个参数.但是它返回了 hello 和一个对象 - Spacebars.kw {hash: Object}

When I added {{testHelper "hello"}}, I expected the console to log hello and null, since nothing was passed as the second parameter. But instead it returned hello and an object - Spacebars.kw {hash: Object}

这是什么 Spacebars.kw {hash: Object}?如果我想让它返回 null 该怎么办?

What is this Spacebars.kw {hash: Object}? What can I do if I want it to return null instead?

推荐答案

Spacebars.kw 包含一个 hash 对象,该对象具有输入参数的哈希值.

Spacebars.kw contains a hash object that has a hash of input parameters.

Meteor 有两种方法来匹配方法,一种是直接匹配,即直接输入参数,例如 {{testHelper "variable1" "variable2" "variable3"}},会匹配up as function(a,b,c) 作为变量 1-3 分别匹配到 a,b 和 c.

Meteor has two methods to match up methods, one is direct matching which is where the parameters are directly input, e.g {{testHelper "variable1" "variable2" "variable3"}}, would match up as function(a,b,c) as variables 1-3 matching up to a,b and c respectively.

第二种输入方法是使用 hash:

The second method of input is using a hash:

{{testHelper a="variable1" b="variable2" c="variable3"}}

这将为 function(a) 提供一个参数,其中 a 是一个 Spacebars.kw 对象.

This would give a single parameter to function(a) where a is a Spacebars.kw object.

Spacebars.kw 对象将有一个名为 hash 的子对象,其结构匹配:

The Spacebars.kw object would have a subobject called hash with a structure that matches:

{ "a" : "variable1",
  "b" : "variable2",
  "c" : "variable3" }

Meteor 将尝试直接匹配第一个参数,但后续参数将作为哈希匹配,以防第二个输入为空,例如在您使用 {{testHelper 'hello'}} 其中 b 将为 null,因此它改为作为哈希值给出.

Meteor will attempt to match up the first param directly, but the subsequent parameters will be matched up as hashes incase the second input is empty such as in the case where you use {{testHelper 'hello'}} where b would be null, so it's given as the hash instead.

它一般是这样给出的,所以如果你得到 b 作为 Spacebars.kw 对象,你可以假设没有第二个输入.另一种方法是您可以使用哈希样式声明,然后直接检查哈希值是否为 null:

Its generically given as this, so if you get b as a Spacebars.kw object, you can assume there was no second input. The alternative is you could use the hash style declarations and then directly check if the hash value is null:

{{testHelper text="Hello"}}
{{testHelper text="Hello" othertext="Hellooo"}}

和助手:

Template.registerHelper('testHelper', function(kw) {
    console.log(kw.hash.text);
    console.log(kw.hash.othertext);
});

这篇关于Meteor - 什么是 Spacebars.kw {hash: Object}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆