The bug source code is here.
Let's say I'm looping over an array in Meteor using an ordinary {{#each}} loop. Each array element is a string, and I output the string every step using {{this}}. So far so good! If I use a handlebars helper to check typeof for this I'll get string. Sweet! Everything is as it should be.
But if I add an {{#if something }}-helper inside the {{#each}} (the something just returns true and thus keeps going and outputs {{this}}) the string will still look good in the HTML, but it is now an object in the typeof check!
This is super-annoying as all the (typeof someVarINeedToTest === 'string') my code might depend on now will return false.
Am I doing something wrong?
Or is this an actual bug?
If so: is it a Meteor-specific or Handlebars-specific bug?
Thanks!
Oh: the source link again. Just pull and run meteor and look in your browser console.
This is because the this variable is supposed to always be an object in JavaScript so when someFunction.apply(someContext); is called in by handlebars, JavaScript turns someContext into an object no matter what it started as. See an example here: http://jsfiddle.net/SyKSE/1/
(In this case, someFunction represents the part of your template that's within the {{#if}} statement.)
A simple (albeit ugly) workaround would be to just always pass your data in as an object, so
['this', 'is', 'an', 'array', 'that', 'we\'re', 'looping', 'through'];
becomes:
[{val: 'this'}, {val: 'is'}, {val: 'an'}, {val: 'array'}, {val: 'that'}, {val: 'we\'re'}, {val: 'looping'}, {val: 'through'}];
And then you'd change your template to look at val instead of this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With