Coffeescript未定义变量

时间:2013-07-02 17:51:41

标签: coffeescript

我有以下coffeescript:

  $("#complete").click ->
    bootbox.dialog "Remember, if you complete the workorder you won't be able to add labor and materials.", [
      label: "Complete"
      class: "btn-success"
      callback: ->
        $("td").filter(':contains("ID:")').each ->
          woid = $(this).nextAll().text()
        $.update "/workorders/" + woid,
         workorder:
           wostatus_id: 232
    ,
     label: "Cancel"
      class: "btn-danger"
      callback: ->
        return 'false'
    ]

运行时,我会在浏览器控制台中看到它:

Uncaught ReferenceError: woid is not defined 

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

变量的范围限定在您首先分配给它们的函数中。要使woid可用,请在过滤器回调之外将其初始化为null

    woid = null

    $("td").filter(':contains("ID:")').each ->
      woid = $(this).nextAll().text()
    $.update "/workorders/" + woid,
     workorder:
       wostatus_id: 232

与往常一样,在调试时检查已编译的JavaScript。答案通常很明显。