coffeescript中回调的范围问题

时间:2016-10-18 06:18:17

标签: coffeescript

我是Coffeescript / Javascript的新手,我正在为Atom编写一个小包。我不知道为什么create_dir函数对内部函数不可见。我怀疑存在某种范围问题,

{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()

我得到的错误:

 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

我已尝试按照此处的建议将所有->更改为=>Multiple Callbacks in CoffeeScript但这并未解决我未定义的函数问题。

1 个答案:

答案 0 :(得分:0)

这是因为使用普通对象而不是类,请参阅@ muistooshort的答案:Anonymous function works as callback but defined function does not