0
저는 Coffeescript/Javascript가 새로 도입되었으며 Atom을위한 작은 패키지를 작성하고 있습니다. create_dir
함수가 내부 함수에 표시되지 않는 이유는 알 수 없습니다. 여기에 제안 내가 =>
모든 ->
을 변경하려고했습니다coffeescript의 콜백 범위 지정 문제
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
: 나는
{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()
오류 내가지고있어, 무슨 문제를 범위 지정의 어떤 종류가 의심 Multiple Callbacks in CoffeeScript하지만 didn를 정의되지 않은 함수로 내 문제를 해결할 수 없습니다.
'module.exports = ImageAssistant = ...'너무 ImageAssistant''에 객체를 던지고있다'@' 물체 안쪽에 무엇이든지 있습니다. 'class ImageAssistant'라고 말하고 싶습니까? –
질문의 들여 쓰기가 파일의 들여 쓰기와 완전히 똑같습니까? 'create_dir'이 너무 낮게 중첩 된 것 같습니다 –
질문에 들여 쓰기를 수정했습니다. 약간 망쳐 놨어. 'create_dir'가 여전히 너무 낮습니까? – tlnagy