【发布时间】:2016-10-24 05:14:47
【问题描述】:
我对 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 "})"
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 但这并没有解决我的函数未定义的问题。
【问题讨论】:
-
module.exports = ImageAssistant = ...只是将一个对象扔到ImageAssistant中,所以对象内部的@就是对象外部的任何内容。你的意思是说class ImageAssistant? -
问题中的缩进是否与您的文件中的完全相同?看起来
create_dir嵌套得太低了一层 -
我修复了问题中的缩进。它有点搞砸了。
create_dir还是太低了吗? -
缩进看起来正确。除了这一用途之外,您是否需要
creat_dir函数?
标签: coffeescript