【问题标题】:Crafty.js not showing correct response using CoffeeScriptCrafty.js 使用 CoffeeScript 未显示正确响应
【发布时间】:2013-08-21 03:46:11
【问题描述】:

我正在使用Crafty.js,我最近添加了一个用于显示帮助信息的按钮,该按钮工作正常,并且我正确地从服务器获得了返回,但我不断收到错误。

解释会很长,请耐心等待。

这是我的代码:

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>
   """

当代码是这样时,它显示的是:undefined,但是,如果我将代码更改为:

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

显示的是:[object Object]

服务器返回的数据是这样的:

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

我错过了什么?

【问题讨论】:

    标签: javascript html coffeescript craftyjs


    【解决方案1】:

    $.getJSON 是异步的,只会返回一个没有message 属性的延迟。您应该将代码附加到回调:

    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>
      """
    

    【讨论】:

    • 我已经复制粘贴了它,但我仍然收到undefined 消息
    • @Sascuash:哎呀,忘了重命名response。现在应该可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多