bindall

作者: hanxianshe_9530 | 来源:发表于2019-10-23 11:50 被阅读0次

_.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}
            />
        );
    }
}

相关文章

  • bindall

    _.bindAll(object, methodNames)绑定一个对象的方法到对象本身,覆盖现有的方法。参数ob...

  • underscore.js中的bind和bindAll

    参考链接:underscore的bindAll和bind到底是什么underscore中的FunctionsUnd...

  • 每日一条JS精华片段:bindAll

    将对象的方法绑定到对象本身,从而覆盖现有方法。 Javascript方法 示例 执行结果 请关注我,每天获得一条精...

网友评论

      本文标题:bindall

      本文链接:https://www.haomeiwen.com/subject/dkohvctx.html