New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template helpers parametres allways typecasted as String #617

Closed
raix opened this Issue Jan 18, 2013 · 7 comments

Comments

Projects
None yet
4 participants
@raix
Contributor

raix commented Jan 18, 2013

I've just completed a generel helper {{sessionEquals}}
There are some issues involved:

{{sessionEquals 'foo' 'bar'}}  //This is ok since its a string
{{sessionEquals 'foo' 1}}  //This returns a string '1' to the helper function
{{sessionEquals 'foo' true}}  //Returns boolean as string 'true' to helper
{{sessionEquals 'foo' ['b', 'a', 'r']}}  //This returns undefined
{{sessionEquals 'foo' {bar:'bar'} }}  //This fails in handlebars

For now I made a workaround for number and boolean - but it creates a bit of unnessesary overheat.

@belisarius222

This comment has been minimized.

belisarius222 commented Jan 18, 2013

I've reproduced this here (using my own sessionEquals function): https://github.com/belisarius222/meteor-session-test

Indeed, the arguments to the helper all show up as strings. My test used a global Handlebars helper, not template-specific.

@raix I'd love to see your workaround for the other types.

@raix

This comment has been minimized.

Contributor

raix commented Jan 18, 2013

Well it's a simple one:

        Handlebars.registerHelper('sessionEquals', function (key, value) {
            var myValue = Session.get(key); //Workaround Issue #617
            if (typeof(myValue) === 'boolean') //Workaround Issue #617
                return Session.equals(key, (value == 'true')); //Workaround Issue #617
            return Session.equals(key, (myValue === +myValue)?+value:value); //Workaround Issue #617
            //return Session.equals(key, value); //When Issue #617 is resolved
        });

Just finished up writing tests and an example for getSession, sessionEquals, find, findOne handlebar helpers - will finish up docs and make it a pull request.

@belisarius222

This comment has been minimized.

belisarius222 commented Jan 18, 2013

Cool! Looking forward to seeing it.

On Fri, Jan 18, 2013 at 3:10 AM, Morten N.O. Nørgaard Henriksen <
notifications@github.com> wrote:

Well it's a simple one:

    Handlebars.registerHelper('sessionEquals', function (key, value) {
        var myValue = Session.get(key); //Workaround Issue #617
        if (typeof(myValue) === 'boolean') //Workaround Issue #617
            return Session.equals(key, (value == 'true')); //Workaround Issue #617
        return Session.equals(key, (myValue === +myValue)?+value:value); //Workaround Issue #617
        //return Session.equals(key, value); //When Issue #617 is resolved
    });

Just finished up writing tests and an example for getSession,
sessionEquals, find, findOne handlebar helpers - will finish up docs and
make it a pull request.


Reply to this email directly or view it on GitHubhttps://github.com//issues/617#issuecomment-12417656.

@raix

This comment has been minimized.

Contributor

raix commented Jan 18, 2013

@belisarius222 Made a meteorite package, https://github.com/raix/Meteor-handlebar-helpers for now until docs in place

@n1mmy

This comment has been minimized.

Member

n1mmy commented Jan 25, 2013

I had a chance to sit down with @dgreensp and look at this today. He agrees this is not the correct behavior, integers and bools should be returned, not strings. [] is special in handlebars and {} is not allowed, so those cases are working as expected.

It appears the conversion to string happens very early on in the processing chain. By the time it comes from our Handlebars.to_json_ast function, it is already a string. This means it is either in the handlebars parser, or our use of its output (packages/handlebars/parse.js)

@raix

This comment has been minimized.

Contributor

raix commented Jan 25, 2013

Thanks, well [] and {}are special cases - They are in the test due to general type testing - its ok that they fail - but when I as a user looks at it, the object/array syntax would make sense (still newbe not enlightened)- but rewriting the whole handlebars or make a new template parser might be overkill - though not impossible.

I'll have a look at the issue later maybe - at the moment the workaround does it's thing, though not optimal.

Btw: I'm writing a bind handler, and I've got one question: can a helper somehow get the template name or object served as an this or template?

At the moment my helper bindStatus needs a param of template name, even though its in a template, seems odd when using it eg.:

   <template name="myTemplate">
   ...
   {{#if bindStatus 'myTemplate'}}

Yeah, I know this stuff should go on SO or IRC? though not much luck there

Example of twoway databinding using the handlebar handlers - only js written is one line pointing the template towards a collection for db access and validation:

<template name="hello">
<!-- New data -->
        <input {{bind 'name'}} placeholder="Name"/>
        <input {{bind 'email'}} type="email" placeholder="Email"/>

        <button {{bindAction 'create'}} class="btn">Add</button>
        <button {{bindAction 'cancel'}} class="btn">Cancel</button>
        <br/>
<!-- Data -->
    {{#each find 'testCol' '{}'}}
        <input {{bind 'name'}} placeholder="Name"/>
        <input {{bind 'email'}} type="email" placeholder="email"/>
       <!-- Pr. Key validation -->
       {{#with bindStatus 'hello' _id 'email'}}
         {{#if this.invalid}} Email is invalid{{/if}}
         {{#if this.required}} Email is required<br/>{{/if}}
       {{/with}}

        <button type="button" {{bindAction 'update'}} class="btn">Update</button>
        <a {{bindAction 'cancel'}} class="btn">Cancel</a>
        <input type="button" value="Delete" {{bindAction 'delete'}} class="btn"/>
        <br/>

       <!-- Pr. Record validation repport -->
       {{#each bindStatus 'hello' _id}}
         {{#if this.invalid}} {{this.keyName}} is invalid<br/>{{/if}}
         {{#if this.required}} {{this.keyName}} is required<br/>{{/if}}
       {{else}}
         No bindStatus / validation errors
       {{/each}}

    {{/each}} <!-- EO find -->

 <!-- Total Validation repport -->
      {{#each bindStatus 'hello'}}
        {{#if this.invalid}} {{this.keyName}} is invalid<br/>{{/if}}
        {{#if this.required}} {{this.keyName}} is required<br/>{{/if}}
      {{else}}
        No bindStatus / validation errors
      {{/each}}
</template>
@gschmidt

This comment has been minimized.

Member

gschmidt commented Feb 1, 2013

Let's continue the discussion of this over at #651 (@raix's pull request)

@gschmidt gschmidt closed this Feb 1, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment