Meteor - What is Spacebars.kw {hash: Object}

I am trying to write a Meteor package that can be placed inside templates. So I first tried to register an assistant.

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

I added the package inside /packages , and in my client template, when I added {{testHelper "hello" "meow"}} , the console registered hello and meow , as I expected.

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

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

+6
source share
1 answer

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

Meteor has two methods for comparing methods: one direct match, in which the parameters are directly entered, for example {{testHelper "variable1" "variable2" "variable3"}} will correspond to function(a,b,c) as variables 1-3 corresponding to a, b and c, respectively.

The second input method uses a hash:

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

This will give a single parameter function(a) , where a is the Spacebars.kw object.

The Spacebars.kw object will have a subobject called hash with a structure that corresponds to:

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

The meteor will try to match the first parameter directly, but subsequent parameters will be mapped as hashes if the second input is empty, for example, if you use {{testHelper 'hello'}} , where b will be null, so this is instead of a hash.

This is generally indicated, so if you get b as a Spacebars.kw object, you can assume that there was no second input. An alternative would be to use hash style declarations, and then directly check if the hash value is null :

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

and assistant:

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

Source: https://habr.com/ru/post/980467/


All Articles