0

I made a template helper called "$" within my Marionette setup

templateHelpers:function(){
    return jQuery.extend(this,{
        '$':function (text){
            return Handlebars.compile('{{'+text+'}}')(this);
        }
    });
},

this allows me to do this

{{$ collection.display}}

which will resolve collection.display, typically to name, but may be some other field name like service_id, and then my handler will resolve that. So I have indirection, and cheaper than typing

{{{{collection.display}}}}

All good. The only snag is, when I stick it inside an {{#each items}} loop,

{{#each items}}
                <option value="{{this.id}}">{{$ ../collection.display}} </option>
{{/each}}

it blows up with

Error: Missing helper: "$"

Note that it doesnt matter what I put after $ when inside the block {{$ anything}} will fail, the helper is just not there.

Mark Lester
  • 363
  • 4
  • 13
  • why use `../collection.display`? specificaly why the double dot notation? i think this is the problem, in any case i dont find this very useful unless i dont get it for the use case – Nikos M. Sep 22 '15 at 21:25
  • you need the .. to get to the parent context, which is where the collection is avaiable, I could go @ROOT. That's not the problem, trust me.. I have a template which I want to use for multiple tables, but the field i want to show in the select list is different. It's usually "name", but i might want to override that in my collection. or model. – Mark Lester Sep 22 '15 at 21:29
  • yeah ok, still i think this is the problem, how to go to root inside the loop in your helper, try to detect double dot in your helper (or check it is actually passed there) and act accordingly – Nikos M. Sep 22 '15 at 21:31
  • maybe the double dot is resolved before passing to your helper. See? – Nikos M. Sep 22 '15 at 21:32
  • It doesnt matter what I put after the $, I tried that, it blows up if I just go {{$ name}}, Handlebars is complaining that it just doesnt have this helper. I am going to have to go to sleep then have a go at seeing how these iterators are implemented, but that doesnt add up either, I cant believe helpers just dont work inside an iterator block. – Mark Lester Sep 22 '15 at 21:34
  • Were you able to find a solution to this? I'm encountering a similar error. – Dakine83 Oct 12 '15 at 21:13
  • My "solution" is below, i.e. register these helpers early. I have submitted an issue to Marionette but it's such a dubious claim and we couldnt replicate so I've withdrawn it for now. I am back at the front end again now and will get to the bottom of this and will update this page and re-comment you once we've got t the bottom of this. – Mark Lester Oct 13 '15 at 07:24

1 Answers1

1

This might be an asynchronicity problem. The body of an iterator block appears to being compiled before my handlers have been applied. Anyway, if I just go

Handlebars.registerHelper('$', function (text){
    return Handlebars.compile('{{'+text+'}}')(this);
});

Before I start declaring any Marionette views, then it works, so not sure if that counts as a bug in marionette, handlebars or of course me.
This is a duplicate of Handlebar helper inside {{#each}}

Community
  • 1
  • 1
Mark Lester
  • 363
  • 4
  • 13