我有一個名爲shapes.coffee一個CoffeeScript的文件:重構的Jquery CoffeeScript的代碼到單獨的文件
jQuery ->
offset = $('#drawing_canvas').offset()
mouse_vertical_position = -Number(offset.top)
mouse_horizontal_position = -Number(offset.left)
canvas = document.getElementById("drawing_canvas")
context = canvas.getContext("2d")
container = canvas.parentNode
temporary_canvas = document.createElement("canvas")
temporary_canvas.id = "temporary_canvas"
temporary_canvas.height = canvas.height
temporary_canvas.width = canvas.width
container.appendChild(temporary_canvas)
temporary_context = temporary_canvas.getContext("2d")
mouse_down_selected = false
$('#temporary_canvas').mousedown (e) ->
mouse_down_selected = true
mouse_horizontal_position = -Number(offset.left)
mouse_vertical_position = -Number(offset.top)
mouse_horizontal_position += e.pageX
mouse_vertical_position += e.pageY
$('body').mouseup ->
mouse_down_selected = false
我想重構一些行成自己的方法(最好是在單獨的文件)。我試圖做到這一點,但我在控制檯中得到方法未定義的錯誤,我一直無法找到涉及jQuery的示例。臨時畫布函數之前的第一組代碼需要在文檔加載時調用。任何例子或建議表示讚賞。
謝謝。
你必須對你的代碼分成可以從domready中回調中被調用的函數。在「包含」中查看此問題http://stackoverflow.com/questions/7718121/is-there-a-way-to-include-file-in-coffee-script –