Change Listener


#1

I’d like to have a callback fired when a TextBuffer’s contents change. So far, I’ve only stumbled into really heavy handed approaches (MutationObserver/keyup events). Does anyone know of the right way to do this?


TernJS plugin development
#2

Per Document Custom Events , this seems to do the job:

module.exports = {
  floobitsView: null,

  activate: function(state) {
    atom.workspaceView.eachEditorView(function(e) {
      var editor = e.getEditor();
      editor.on("contents-modified", function(){
        process.nextTick(function(){
          console.log('modified')
        })
      })
    })
  },
  deactivate: function() {
  },
  serialize: function() {
  }
};

#3

I am not sure if there are a set of events documented somewhere, but I think you can subscribe to certain Atom editor events and feed it callbacks to trigger when the event occurs. One example I found in the status-bar package looks like the following:

initialize: (@statusBar) ->
  @subscribe @statusBar, 'active-buffer-changed', @updateCursorPositionText
  @subscribe atom.workspaceView, 'cursor:moved', @updateCursorPositionText

This code is found in cursor-position-view.coffee.

Anyone know where a listing of subscribable events can be found?


#4

I saw this posted somewhere.


#5

A more complete list in action can be found here.