coffeescript中的回调问题 [英] Scoping issue with callbacks in coffeescript

查看:191
本文介绍了coffeescript中的回调问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我得到的错误:

 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

我已经尝试将所有 - > 更改为 => ,如下所示: CoffeeScript中的多个回调但是没有解决我的问题,该函数未定义。

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.

推荐答案

这是因为使用一个简单对象而不是类,请参见@ muistooshort的答案:匿名函数作为回调函数,但定义函数不

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

这篇关于coffeescript中的回调问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆