andreypopp / autobind-decorator

Decorator to automatically bind methods to class instances
MIT License
1.45k stars 66 forks source link

Supports filtering which methods are bound #27

Closed cpsubrian closed 8 years ago

cpsubrian commented 8 years ago

Solves #20 and can help avoid some collisions with React tooling. For example, I'm having issues with the getters colliding with some react babel transforms.

With this, someone could easily publish a react-autobind-decorator that would look like the following:

import autobind from 'autobind-decorator'
export default autobind(/^(render)|(shouldComponentUpdate)|(component.*))$/, true)

Or you might only want to bind event 'handlers':

@autobind(/^handle/)
class Component {
  // Bound
  handleClick(e) {}

  // Not Bound
  theBusinessLogic() {}
}
stevemao commented 8 years ago

Thanks @cpsubrian for this linking https://twitter.com/dan_abramov/status/778948609752662016 I was thinking this would be a bit complex. Why don't you bind each individual method?

cpsubrian commented 8 years ago

FWIW I don't think whatever the initial problem is affects me anymore (my build stack changes with every project heh).

stevemao commented 8 years ago

I'm gonna close this .. I think this looks like an interesting idea. Feel free to comment though!