Core:save from mini TextEditorView


Hello everybody,
I am currently working with two packages. One of them intercepts all the core:save events and performs some actions based on the target (so far, it works with atom-text-editor as target), like this:

@subscriptions.add atom.commands.onDidDispatch (event) =>
  if event.type is 'core:save'
    savedPath = if then else null
    @doWhateverWithFileAt savedPath
  else if event.type is 'tree-view:remove'
    # do something else with removed files

The second package opens files into a mini text editor created with atom-space-pen-views and allows the user to edit and save such files.

@content: (params) =>
  @subview 'extraView', new TextEditorView(mini: true, placeholderText: '...')
    @div class: "btn-group", =>
    @button outlet: 'openButton', click: 'openExtra', class: 'btn btn-sm btn-primary', 'Open in new tab'
    @button outlet: 'saveButton', click: 'saveExtra', class: 'btn btn-sm disabled', 'Save changes'


saveExtra: ->
  # variable @extraFilePath contains the path of the open file
  return unless @extraFilePath

  toBeSaved = @extraView.getText()
  @extraFileCtnt = @extraView.getText()
  fs.writeFileSync @extraFilePath, toBeSaved
  @toggleSaveButton false

Point is, if I save the file with fs, the core event is not fired and the first package cannot possibly catch it.
My question is, is there a way to dispatch a core:save event so that it is properly caught? I tried with atom.commands.dispatch @extraView, 'core:save' but it does not seem to work.

Thank you all!


My guess is that atom.commands.dispatch needs a DOM Node but you’re calling it with a View (which is a jQuery). You could try atom.commands.dispatch @extraView[0], 'core:save'.


I’ll give it a try asap, thanks a lot for the tip :wink:


Why are you intercepting core:save? Does the TextBuffer.onWillSave event not work for your use case? (Intercepting core:save could screw up the behavior of other packages or Atom itself.)


@deprint: I just quickly gave it a try, no exception is thrown bust still the event is not caught. I’ll keep looking into that but thank you, I did not realize the node was wrapped into a jquery object.

@leedohm: I’m intercepting the save events because that’s a behavior I’d like to have on all the buffers. Should I watch the opening of a new TextBuffer instead? So far, I did not have any issue with other packages, but I totally see your point. This was some sort of a hack I put in place to avoid using a watcher on the entire project.


I would watch for opening of TextEditors and use TextBuffer.onWillSave. This also has the added benefit that if a package tells a TextBuffer to save itself without a core:save being raised by the user, your package will still do its thing. That is of course assuming that you would want to do your onWillSave handling for situations other than direct user interaction.