Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18359
| Message-ID | <9385853.fL9pMimAyI@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-02-06 04:15 +0100 |
| Subject | Re: handlebars javascript error on return in function, i is not defined |
| Newsgroups | comp.lang.javascript |
| References | <ab80af3d-ae71-455f-a5de-e9e67a0ba2f9@googlegroups.com> |
Not usually reading Googlodytes, but FWIW …
jr wrote:
> I want to print a simple list using a js template in this order:
There is no such thing as a “js template”. There is no single “js” language
to begin with.
> User, product list, benefit list, status.
> I get a reference error on the value returned.
> ReferenceError: i is not defined
>
>
> I can tell what the problem is but I don't know how to solve it.
> I need to use the {{#each}}{{/each}} function to count the number of
> products and benefits. I just want to add the count of i on each, {{each}}
> block to the returned options and context . I am not sure what this line
> returns but I dont' need the values returned just the count.
> ret = ret + options.fn(context[i]);
> <html>
> <head>
> <script type="text/javascript"
> src="js/vendor/handlebars-1.0.rc.1.js"></script> <script
> type="text/x-handlebars-template">
“text/x-handlebars-template” is not a registered MIME media type. If it
works at all in a user agent then the scripting language used is, at best, a
very distant relative of ECMAScript and therefore very probably off-topic
here.
<http://www.w3.org/TR/2012/CR-
html5-20121217/scripting-1.html#scriptingLanguages>
> […] Handlebars.registerHelper('each', function(context, options) {
> var ret = "";
>
> for(var i=0, j=context.length; i<j; i++) {
> ret = ret + options.fn(context[i]);
> product:i;
> benefit:i;
In a conforming implementation of ECMAScript without syntax extensions that
would be two /LabelledStatement/s where each evaluates to the value of ”i”
without further effect. In the “text/x-handlebars-template” scripting
language, however, it may mean anything.
> }
>
> return {ret, product:i, benefit:i};
There is no telling (without RTFM, which you should have done) how this
rather obscure scripting language works. For example, it is possible that,
contrary to strict implementations of ECMAScript Ed. 5.x, it uses block
scoping which would cause “i” to be unavailable outside of the loop because
it was declared inside of it. Which explain the ReferenceError exception.
On the other hand, you may have simply made a copy-and-paste error again.
You have a long record on Usenet to be challenged by even the simplest
programming tasks. I wonder, why are you repeatedly and continuously
choosing the most obscure tools (here: *handlebars* *Release Candidate* 1)
to achieve what you want? Why not finally learn this craft from the ground
up, starting with simple standards-compliant code that you have actually
written yourself?
<https://developer.mozilla.org/en-US/docs/>
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
handlebars javascript error on return in function, i is not defined jr <jlrough@yahoo.com> - 2013-02-05 15:05 -0800
Re: handlebars javascript error on return in function, i is not defined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-06 04:15 +0100
Re: handlebars javascript error on return in function, i is not defined JRough <janis.rough@gmail.com> - 2013-02-06 10:24 -0800
Re: handlebars javascript error on return in function, i is not defined Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-02-07 00:27 +0100
csiph-web