Question

I'm using Crafty.js, I recently added a button that is meant to show help info, the button works fine, and I get the return from the server correctly, but I keep getting errors.

It'll be long to explain, so be patient.

This is my code:

where = this._current
auxiliary = $.getJSON('/help/', {'scene': where})

Crafty.e("HTML")
.attr({x: 100, y: 200, w: 224, h: 200})
.replace """
  <font color="white">
    #{auxiliary.message}
  </font>
   """

When the code is like that, what it shows is: undefined, but, if I change the code like:

Crafty.e("HTML")
.attr({x: 100, y: 200, w: 824, h: 400})
.replace """
  <font color="white">
    #{auxiliary}
  </font>
   """

What it shows is: [object Object]

The data that the server is returning is like this:

{
 "message":
   "<p>Help text</p>",
 "result":
   "ok"
}

What am I missing?

Was it helpful?

Solution

$.getJSON is asynchronous and will just return a deferred, which has no message property. You should attach your code to a callback:

where = this._current

$.getJSON('/help/', {'scene': where}).done (response) ->
  Crafty.e("HTML")
  .attr({x: 100, y: 200, w: 224, h: 200})
  .replace """
    <font color="white">
      #{response.message}
    </font>
  """
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top