Vinay
Vinay

Reputation: 6322

Passing Controller Model Properties to Handlebars Helpers

I am trying to use a fairly simple Handlebars helper within an #each loop going over the items in a controller's model (it's a collection of models supplied by EmberData using the fixtureAdapter). Here's the basic layout of the template:

{{#each}}
    ....
    {{#each clusters}}
        <span>{{prettifyTimestampTime cluster_timestamp}}</span>
    {{/each}}
{{/each}}

Here is my helper (in coffeescript):

Ember.Handlebars.registerBoundHelper 'prettifyTimestampTime', (timestamp, options) ->
    d = new Date timestamp
    hours = String(d.getHours() % 12)
    hours = "0#{hours}" if hours.length is 1
    "#{hours}:#{d.getMinutes()}:#{d.getMinutes()}" 

I originally set this helper on Handlebars itself with Handelbars.registerHelper, but I kept getting the string "cluster_timestamp" passed in (no matter what I put after prettifyTimestampTime in the template, it would get resolved to a String).

I then followed suit and attempted to give stukennedy's answer a try by wrapping the property in quotes and doing a lookup on options.data.keywords, but the key wasn't in that dictionary.

When I to tried to use Ember.Handlebars.registerBoundHelper instead, per Bradley Preist's suggestion here, the timestamp argument is simply undefined. I do notice that, when I try to access any properties on options.contexts[0], the values they point to are undefined, but the keys are there.

I feel completely lost at this point. Any direction is welcome. Is this really a known bug in Ember, as stukennedy has pointed out in the previous SO questions? Having just started with Ember and Handlebars, I would rather have this just be some dumb error on my end, considering how difficult it was for me to also to set up fixtures with Ember data in the first place. :-P

EDIT: After seeing this question, I realize why registerHelper did not work (because it does not try to associate what is passed in with the property of the current object in context). However, I'm still just as lost since the lookup of the property isn't working. Perhaps this is an Ember Data issue with promises? The only thing that makes me confused about that being the case is that I am using fixtures (no request made), and I am able to get at the property cluster_timestamp normally with a normal expression like {{cluster_timestamp}}.

Upvotes: 1

Views: 1468

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

don't use registerHelper, http://emberjs.com/guides/templates/writing-helpers/

Ember.Handlebars.helper 'prettifyTimestampTime', (timestamp, options) ->
    d = new Date timestamp
    hours = String(d.getHours() % 12)
    hours = "0#{hours}" if hours.length is 1
    "#{hours}:#{d.getMinutes()}:#{d.getMinutes()}" 

Upvotes: 1

Related Questions