Add EventListener on whole editor for Save event, not just current view


#1

I have a problem with adding a callback on the Save event.
I want to add it to the editor, to be able to catch the Save event in all views including those not already opened, without having to add it manually for each.

I have this code, I would like to do the same for everything.

  activeView = atom.workspaceView.getActiveView()
  activeView.preempt 'core:save', (e) =>
    this.updateHeader(false)

#2

Something like this should work:

atom.workspace.eachEditor (editor) =>
  editor.on 'will-be-saved', =>
    @updateHeader(false)

In CoffeeScript the @ is shorthand for this. and is the canonical way to write it.


#3

Hello boys,

I am trying to handle the will-be-saved event…
But I want to do that directly in my index.js, not in the coffee file…

I don’t know the correct syntax to do that anyone can help me ? I have read your answer Leedohm but I am not comfortable with the Atom syntax for coffee and js :wink:


#4

You can always go to the Try CoffeeScript tab at http://coffeescript.org/ and paste in the CoffeeScript. It will translate it into JavaScript for you.


#5

Hey men you do a mistake :wink:

atom.workspace.eachEditor (editor) =>
    editor.buffer.on 'will-be-saved', =>
       @updateHeader(false)

You forgot to select the buffer of the editor to listen to an event :slight_smile:

I do this just for my neighbors :wink:

Ps: Thank you for http://coffeescript.org/