_.bindAll(object, methodNames)
绑定一个对象的方法到对象本身,覆盖现有的方法。
参数
object (Object): 用来绑定和分配绑定方法的对象。
methodNames (...(string|string[])): 对象绑定方法的名称。
返回
(Object): 返回 object.
例子
var view = {
'label': 'docs',
'click': function() {
console.log('clicked ' + this.label);
}
};
_.bindAll(view, ['click']);
jQuery(element).on('click', view.click);
// => Logs 'clicked docs' when clicked.
class TitledComponent extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleUpdateProjectTitle'
]);
this.state = {
projectTitle: null
};
}
handleUpdateProjectTitle (newTitle) {
this.setState({projectTitle: newTitle});
}
render () {
return (
<WrappedComponent
canEditTitle
projectTitle={this.state.projectTitle}
onUpdateProjectTitle={this.handleUpdateProjectTitle}
{...this.props}
/>
);
}
}
class CameraModal extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleAccess',
'handleBack',
'handleCancel',
'handleCapture',
'handleLoaded',
'handleSubmit',
'setCanvas'
]);
this.video = null;
this.videoDevice = null;
this.state = {
capture: null,
access: false,
loaded: false
};
}
componentWillUnmount () {
if (this.videoDevice) {
this.videoDevice.disableVideo();
}
}
handleAccess () {
this.setState({access: true});
}
handleLoaded () {
this.setState({loaded: true});
}
handleBack () {
this.setState({capture: null});
this.videoDevice.clearSnapshot();
}
handleCapture () {
if (this.state.loaded) {
const capture = this.videoDevice.takeSnapshot();
this.setState({capture: capture});
}
}
setCanvas (canvas) {
this.canvas = canvas;
if (this.canvas) {
this.videoDevice = new ModalVideoManager(this.canvas);
this.videoDevice.enableVideo(this.handleAccess, this.handleLoaded);
}
}
handleSubmit () {
if (!this.state.capture) return;
this.props.onNewCostume(this.state.capture);
this.props.onClose();
}
handleCancel () {
this.props.onClose();
}
render () {
return (
<CameraModalComponent
access={this.state.access}
canvasRef={this.setCanvas}
capture={this.state.capture}
loaded={this.state.loaded}
onBack={this.handleBack}
onCancel={this.handleCancel}
onCapture={this.handleCapture}
onSubmit={this.handleSubmit}
/>
);
}
}
网友评论