Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Added options bindOnly & wontBind for more control
Browse files Browse the repository at this point in the history
  • Loading branch information
ffxsam committed Mar 17, 2016
1 parent a6433c0 commit e7c55c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,38 @@ import autoBind from 'react-autobind';
You will generally call autoBind from the class constructor, passing the 'this' context:

```javascript
constructor() {
super();
constructor(props) {
super(props);
autoBind(this);
}
```

Autobind is smart enough to avoid binding React related methods (such as render and lifecycle hooks).

You can also explicitly specify which methods you want to bind:
You can also explicitly exclude certain methods to exclude from binding:

```javascript
constructor() {
super();
autoBind(this, 'myMethod1', 'myMethod2');
constructor(props) {
super(props);
autoBind(this, {
wontBind: ['leaveAlone1', 'leaveAlone2']
});
}
```

Or specify only certain methods you want to bind:

```javascript
constructor(props) {
super(props);
autoBind(this, {
bindOnly: ['myMethod1', 'myMethod2']
});
}
```

Naturally, `wontBind` and `bindOnly` cannot be used together.

### Example

```javascript
Expand All @@ -54,8 +69,8 @@ import autoBind from 'react-autobind';

class App extends React.Component {

constructor() {
super();
constructor(props) {
super(props);
this.state = {
clickCounter: 0
}
Expand Down
12 changes: 8 additions & 4 deletions lib/autoBind.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ function autoBind(context) {
return;
}

var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var objPrototype = Object.getPrototypeOf(context);

if (arguments.length > 1) {
// If a list of methods to bind is provided, use it.
toBind = Array.prototype.slice.call(arguments, 1);
if (options.bindOnly) {
// If we want to bind *only* a set list of methods, then do that (nothing else matters)
toBind = options.bindOnly;
} else {
// If no list of methods to bind is provided, bind all available methods in class.
// Otherwise, bind all available methods in the class
toBind = Object.getOwnPropertyNames(objPrototype);

// And exclude anything explicitly passed in wontBind
wontBind = wontBind.concat(options.wontBind || []);
}

toBind.forEach(function (method) {
Expand Down
14 changes: 9 additions & 5 deletions src/autoBind.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const wontBind = [
let wontBind = [
'constructor',
'render',
'componentWillMount',
Expand All @@ -18,14 +18,18 @@ export default function autoBind (context) {
return;
}

const options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
let objPrototype = Object.getPrototypeOf(context);

if(arguments.length > 1) {
// If a list of methods to bind is provided, use it.
toBind = Array.prototype.slice.call(arguments, 1);
if(options.bindOnly) {
// If we want to bind *only* a set list of methods, then do that (nothing else matters)
toBind = options.bindOnly;
} else {
// If no list of methods to bind is provided, bind all available methods in class.
// Otherwise, bind all available methods in the class
toBind = Object.getOwnPropertyNames(objPrototype);

// And exclude anything explicitly passed in wontBind
wontBind = wontBind.concat(options.wontBind || []);
}

toBind.forEach(function(method) {
Expand Down

0 comments on commit e7c55c5

Please sign in to comment.