What am I doing wrong here? I’m using the fat arrows => for my callbacks, however when the code reaches cb.onEndElement and calls @returner I get an null object exception. So why doesn’t @returner exist?
class Parser
constructor: () ->
@returner = (data) ->
searchParser: new xml.SaxParser (cb) =>
cb.onStartElementNS (elem, attrs, prefix, url, ns) =>
if elem is "results" then @results = []
else if elem is "title" then @curr = "title"
else @curr = "none"
cb.onCdata (cdata) =>
if @curr is "title" then @book.title = cdata
cb.onEndElementNS (elem, prefix, url) =>
@results.push @book if elem is "book"
cb.onEndDocument =>
@returner @results
search: (str, callback) ->
@returner = callback
@searchParser.parseString str
p = new Parser
p.search "somexml", (data) ->
console.log JSON.stringify data
Your method
searchneeds a fat-arrow=>in order to bind it to instances ofParser.Additionally, although the line
searchParser: new xml.SaxParser (cb) =>compiles, it’s probably not doing what you want, because the fat arrow is binding the callback toParserand notthis. You have two options:@searchParser = new xml.SaxParser (cb) => ...in your constructor instead, given the way you are calling it.searchParser: () => new xml.SaxParser (cb) =>and call it with parens lower down:@searchParser().parseString str, which would create asearchParsermethod bound tothisAs an example, here are my two solutions, as well as your original line, slightly simplified, as well as the compiled code, for compare and contrast purposes:
Simplified example in CoffeeScript:
Compiled JavaScript:
Note how
searchParser1andsearchParser2have their callbacks bound tothisandsearchParser‘s is bound toParser.As always, the “Try CoffeeScript” button at the CoffeeScript homepage (http://jashkenas.github.com/coffee-script/) is your friend!