Reddirt
Reddirt

Reputation: 5953

Coffeescript undefined variable

I have the following 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'
    ]

When it runs, I get this in the browser console:

Uncaught ReferenceError: woid is not defined 

Thanks for the help!

Upvotes: 0

Views: 138

Answers (1)

user229044
user229044

Reputation: 239240

Variables are scoped to the function where you first assign to them. To make woid available, initialize it to null outside your filter callback:

    woid = null

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

And as always, check your compiled JavaScript when debugging. The answer will usually be quite obvious.

Upvotes: 1

Related Questions