When I use Handlebars.compile on a variable it works well, but when I put the function on a Coffee class property, it give me undefined , bug? Or I miss some basic java/coffee functionality?
There is my fiddle: http://jsfiddle.net/Cl0udW4lk3r/BpY5b/
class Test
template: Handlebars.compile (
"""
{{#iterate data}}
<p>{{data}}</p>
{{/iterate}}
"""
)
template = Handlebars.compile (
"""
{{#iterate data}}
<p>{{data}}</p>
{{/iterate}}
"""
)
There is another fiddle with backbone in action: http://jsfiddle.net/Cl0udW4lk3r/D3FR9/20/
---- UPDATE -----
My first fiddle is solved (but the handlebars version was outdated), and the error was a stupid OOP inattention...
But! My second fiddle (and now that I've updated the first fiddle's handlebars resource, also my first fiddle..) give to me an error
TypeError: callback is not a function
It seems that the callback part is not right processed... the error is the same both on coffee class or simple variable case...
When you say this:
class Test
template: ...
you're defining template
as an instance property so you'd have to create an instance before you could look at template
:
t = new Test
console.log(t.template)
Or you could dig the template
out of Test
's prototype:
console.log(Test::template)
If you want template
to be a class property then:
class Test
@template: ...
Demo: http://jsfiddle.net/ambiguous/tvDpN/
Your second issue is related to a change in how Handlebars helpers get called. The last argument used to be the callback function and you'd use fn.inverse
for the {{else}}
block. The last argument to the helper is now an object with the function in .fn
and the else-block function in .inverse
. So for the latest versions of Handlebars, your helper:
Handlebars.registerHelper 'iterate', (context, callback) ->
switch typeof context
when 'object' then (callback(key: key, value: value) for own key, value of context).join ''
should looks more like this:
Handlebars.registerHelper 'iterate', (context, options) ->
switch typeof context
when 'object' then (options.fn(key: key, value: value) for own key, value of context).join ''
It's because you're inspecting the template
property of the Test
class itself, instead of instantiating a new instance of the Test
class and inspecting the property on that instance. The following should work:
$ ->
console.log new Test().template
console.log template
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.