tlnagy tlnagy - 1 month ago 8
CoffeeScript Question

Scoping issue with callbacks in coffeescript

I'm really new to Coffeescript/Javascript and I'm writing a little package for Atom. I have no idea why the

create_dir
function is not visible to the inner function. I suspect there is some kind of scoping problem going on,

{CompositeDisposable, Directory, File} = require 'atom'

module.exports = ImageAssistant =
subscriptions: null

activate: (state) ->
# Events subscribed to in atom's system can be easily cleaned up
# with a CompositeDisposable
@subscriptions = new CompositeDisposable

# Register command that toggles this view
@subscriptions.add atom.workspace.observeTextEditors((editor) ->
textEditorElement = atom.views.getView(editor)

# on drag and drop event
textEditorElement.addEventListener("drop", (e) ->
e.preventDefault?()
e.stopPropagation?()

editor = atom.workspace.getActiveTextEditor()
return unless editor

dropped_files = e.dataTransfer.files
target_file = editor.getPath()

fs = require 'fs'
path = require 'path'
crypto = require "crypto"

assets_path = path.join(target_file, "..", "assets")

for f in dropped_files
if fs.lstatSync(f.path).isFile()
buffer = new Buffer(fs.readFileSync(f.path))
md5 = crypto.createHash 'md5'
md5.update(buffer)

img_filename = "#{path.parse(target_file).name}-#{md5.digest('hex').slice(0,8)}#{path.extname(f.path)}"
console.log img_filename

assets_dir = new Directory(assets_path)

@create_dir assets_dir, ()=>
fs.writeFile path.join(assets_dir, img_filename), buffer, 'binary', ()=>
console.log "Copied file over to #{assets_dir}"
editor.insertText "![](#{path.join("assets", img_filename)})"

return false
)
)

create_dir: (dir_path, callback)=>
dir_handle = new Directory(dir_path)

dir_handle.exists().then (existed) =>
if not existed
dir_handle.create().then (created) =>
if created
console.log "Creation of #{dir_path} successful"
callback()
else
callback()

deactivate: ->
@subscriptions.dispose()


The error I'm getting:

atom-markdown-image-assistant.coffee:43 Uncaught TypeError: _this.create_dir is not a function(anonymous function) @ atom-markdown-image-assistant.coffee:43
/Applications/Atom.app/Contents/Resources/app.asar/node_modules/jquery/dist/jquery.js:8630 GET https://atom.io/api/packages/markdown-image-assistant 404 (Not Found)send @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/jquery/dist/jquery.js:8630jQuery.extend.ajax @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/jquery/dist/jquery.js:8166(anonymous function) @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/user-utilities.js:258module.exports.getLatestPackageData @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/user-utilities.js:257module.exports.checkPackageUpToDate @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/user-utilities.js:271NotificationElement.renderFatalError @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/notification-elem…:204NotificationElement.render @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/notification-elem…:159NotificationElement.initialize @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/notification-elem…:50(anonymous function) @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/main.js:89module.exports.ViewRegistry.createView @ /Applications/Atom.app/Contents/Resources/app.asar/src/view-registry.js:119module.exports.ViewRegistry.getView @ /Applications/Atom.app/Contents/Resources/app.asar/src/view-registry.js:86Notifications.addNotificationView @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/main.js:126(anonymous function) @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/main.js:27module.exports.Emitter.simpleDispatch @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/event-kit/lib/emitter.js:25module.exports.Emitter.emit @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/event-kit/lib/emitter.js:125module.exports.NotificationManager.addNotification @ /Applications/Atom.app/Contents/Resources/app.asar/src/notification-manager.js:54module.exports.NotificationManager.addFatalError @ /Applications/Atom.app/Contents/Resources/app.asar/src/notification-manager.js:45(anonymous function) @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/notifications/lib/main.js:53module.exports.Emitter.simpleDispatch @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/event-kit/lib/emitter.js:25module.exports.Emitter.emit @ /Applications/Atom.app/Contents/Resources/app.asar/node_modules/event-kit/lib/emitter.js:125(anonymous function) @ /Applications/Atom.app/Contents/Resources/app.asar/src/atom-environment.js:837


I've tried changing all
->
to
=>
as suggested here: Multiple Callbacks in CoffeeScript but that didn't fix my issue with the function being undefined.

Answer

This is due to using a plain object instead of class, see @muistooshort's answer here: Anonymous function works as callback but defined function does not