Skip to content

Instantly share code, notes, and snippets.

@bobrocke
Created May 26, 2015 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobrocke/84e5609afd8f0bc08fcb to your computer and use it in GitHub Desktop.
Save bobrocke/84e5609afd8f0bc08fcb to your computer and use it in GitHub Desktop.
Atom Command Registration
activate: function(state) {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.commands.add('.platform-darwin .tree-view', {
'open-a-terminal:openroot': (function(_this) {
return function() {
return _this.openroot();
};
})(this)
}));
this.subscriptions.add(atom.commands.add('.platform-darwin .tree-view', {
'open-a-terminal:openhere': (function(_this) {
return function() {
return _this.openhere();
};
})(this)
}));
},
@bobrocke
Copy link
Author

From Thomas Johansen:

I'm 99% sure you can't evaluate functions when creating a object literal; just capture the this in the closure. So instead of doing the nested function approach to capture it, do var self = this; before adding the commands, and use e.g. self.openroot();.

Thomas Johansen [12:21 AM]
Better yet would be to use ES6 (i.e. Babel), then you could replace that yucky function with an arrow function; open-a-terminal:openroot: () => { this.openroot(); }`

@bobrocke
Copy link
Author

From Michael Bolin:

() => this.openroot()` is sufficient in ES6 when the body of the function is a single expression

@bobrocke
Copy link
Author

This is what worked:

    activate: function(state) {
        var self = this;

        this.subscriptions = new CompositeDisposable();

        this.subscriptions.add(
            atom.commands.add(
                '.tree-view',
                {
                    'open-a-terminal:openroot': function() { return self.openroot();},
                    'open-a-terminal:openhere': function() { return self.openhere();}
                }
            )
        );
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment