https://github.com/coobaha/babel-plugin-class-autobind
Binds prefixed class handlers
https://github.com/coobaha/babel-plugin-class-autobind
autobind babel-plugin
Last synced: 5 months ago
JSON representation
Binds prefixed class handlers
- Host: GitHub
- URL: https://github.com/coobaha/babel-plugin-class-autobind
- Owner: Coobaha
- License: mit
- Created: 2016-01-04T20:16:46.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-07-20T05:37:57.000Z (about 8 years ago)
- Last Synced: 2025-04-14T18:53:12.618Z (6 months ago)
- Topics: autobind, babel-plugin
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# babel-plugin-class-autobind
**Please note that this will add implicit magic to your code**
This plugin will autobind all handlers for components listed in directive '@autobind'
prefixes = `on, _on, handle, _handle`;
## Example**In**
```js
'@autobind Component';class Component {
constructor() {
}
handleMe(){}
onMe(){}
noBind(){}
// @autobind-ignore
onIgnored(){}
}
```**Out**
```js
class Component {
constructor() {
this.handleMe = this.handleMe.bind(this);
this.onMe = this.onMe.bind(this);
}
handleMe() {}
onMe() {}
noBind() {}
onIgnored() {}
}
```## Installation
```sh
$ npm install babel-plugin-class-autobind
```
## Optionsif no components are listed in directive - all handlers in file classes will be bound to class instance. (equal to `@autobind *`)
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["class-autobind"]
}
```### Via CLI
```sh
$ babel --plugins class-autobind script.js
```### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["class-autobind"]
});
```