Getting more information in callback


#1

Hi All,

I’m fairly new to JS and its use of callbacks.

If I register a callback - for example, onDidChangeGrammar - it receives some related object as an argument when fired, in this case, the new Grammar.

How do I find the affected editor here to perform any additional processing? It seems pointless to just get the Grammar itself without any context. Surely I’ve missed something obvious here…?


#2

Since onDidChangeGrammar() is a TextEditor method, you can’t register a callback for it unless you have an editor. When you’re inside the callback, you’ll still have access to the editor because you’re inside the scope in which your editor has been assigned. For an example of how this works, add this to your init.cson:

atom.workspace.observeTextEditors (editor) ->
    editor.onDidChangeGrammar (grammar) ->
        console.log(editor)

#3

Never mind, I guess it was obvious - too long staring at the problem.
For others:

# within activate()
this.subscriptions.add(atom.workspace.observeTextEditors(module.exports.grammarChangeAttacher));

# later on
grammarChangeAttacher(editor) {
    module.exports.subscriptions.add(editor.onDidChangeGrammar(function(grammar) {
        module.exports.grammarHandler(grammar, editor);
    }));
},

@DamnedScholar - Tx :stuck_out_tongue: beat me by minutes…