UNPKG

2.35 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = bindAutoBindMethods;
7/**
8 * Copyright 2013-2015, Facebook, Inc.
9 * All rights reserved.
10 *
11 * This source code is licensed under the BSD-style license found in the
12 * LICENSE file in the root directory of React source tree. An additional grant
13 * of patent rights can be found in the PATENTS file in the same directory.
14 *
15 * Original:
16 * https://github.com/facebook/react/blob/6508b1ad273a6f371e8d90ae676e5390199461b4/src/isomorphic/classic/class/ReactClass.js#L650-L713
17 */
18
19function bindAutoBindMethod(component, method) {
20 var boundMethod = method.bind(component);
21
22 boundMethod.__reactBoundContext = component;
23 boundMethod.__reactBoundMethod = method;
24 boundMethod.__reactBoundArguments = null;
25
26 var componentName = component.constructor.displayName,
27 _bind = boundMethod.bind;
28
29 boundMethod.bind = function (newThis) {
30 var args = Array.prototype.slice.call(arguments, 1);
31 if (newThis !== component && newThis !== null) {
32 console.warn('bind(): React component methods may only be bound to the ' + 'component instance. See ' + componentName);
33 } else if (!args.length) {
34 console.warn('bind(): You are binding a component method to the component. ' + 'React does this for you automatically in a high-performance ' + 'way, so you can safely remove this call. See ' + componentName);
35 return boundMethod;
36 }
37
38 var reboundMethod = _bind.apply(boundMethod, arguments);
39 reboundMethod.__reactBoundContext = component;
40 reboundMethod.__reactBoundMethod = method;
41 reboundMethod.__reactBoundArguments = args;
42
43 return reboundMethod;
44 };
45
46 return boundMethod;
47}
48
49function bindAutoBindMethods(component) {
50 for (var autoBindKey in component.__reactAutoBindMap) {
51 if (!component.__reactAutoBindMap.hasOwnProperty(autoBindKey)) {
52 return;
53 }
54
55 // Tweak: skip methods that are already bound.
56 // This is to preserve method reference in case it is used
57 // as a subscription handler that needs to be detached later.
58 if (component.hasOwnProperty(autoBindKey) && component[autoBindKey].__reactBoundContext === component) {
59 continue;
60 }
61
62 var method = component.__reactAutoBindMap[autoBindKey];
63 component[autoBindKey] = bindAutoBindMethod(component, method);
64 }
65};
\No newline at end of file