2013-07-02 59 views
0

我有以下的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

變量的作用域爲您首先分配給它們的函數。爲了使woid可用時,它初始化爲null您的過濾器回調外:

woid = null 

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

和往常一樣,檢查調試時您編譯的JavaScript。答案通常很明顯。

+0

感謝您的幫助! – Reddirt