react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / ReactOwner.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule ReactOwner9*/1011"use strict";1213var emptyObject = require('emptyObject');14var invariant = require('invariant');1516/**17* ReactOwners are capable of storing references to owned components.18*19* All components are capable of //being// referenced by owner components, but20* only ReactOwner components are capable of //referencing// owned components.21* The named reference is known as a "ref".22*23* Refs are available when mounted and updated during reconciliation.24*25* var MyComponent = React.createClass({26* render: function() {27* return (28* <div onClick={this.handleClick}>29* <CustomComponent ref="custom" />30* </div>31* );32* },33* handleClick: function() {34* this.refs.custom.handleClick();35* },36* componentDidMount: function() {37* this.refs.custom.initialize();38* }39* });40*41* Refs should rarely be used. When refs are used, they should only be done to42* control data that is not handled by React's data flow.43*44* @class ReactOwner45*/46var ReactOwner = {4748/**49* @param {?object} object50* @return {boolean} True if `object` is a valid owner.51* @final52*/53isValidOwner: function(object) {54return !!(55object &&56typeof object.attachRef === 'function' &&57typeof object.detachRef === 'function'58);59},6061/**62* Adds a component by ref to an owner component.63*64* @param {ReactComponent} component Component to reference.65* @param {string} ref Name by which to refer to the component.66* @param {ReactOwner} owner Component on which to record the ref.67* @final68* @internal69*/70addComponentAsRefTo: function(component, ref, owner) {71invariant(72ReactOwner.isValidOwner(owner),73'addComponentAsRefTo(...): Only a ReactOwner can have refs. This ' +74'usually means that you\'re trying to add a ref to a component that ' +75'doesn\'t have an owner (that is, was not created inside of another ' +76'component\'s `render` method). Try rendering this component inside of ' +77'a new top-level component which will hold the ref.'78);79owner.attachRef(ref, component);80},8182/**83* Removes a component by ref from an owner component.84*85* @param {ReactComponent} component Component to dereference.86* @param {string} ref Name of the ref to remove.87* @param {ReactOwner} owner Component on which the ref is recorded.88* @final89* @internal90*/91removeComponentAsRefFrom: function(component, ref, owner) {92invariant(93ReactOwner.isValidOwner(owner),94'removeComponentAsRefFrom(...): Only a ReactOwner can have refs. This ' +95'usually means that you\'re trying to remove a ref to a component that ' +96'doesn\'t have an owner (that is, was not created inside of another ' +97'component\'s `render` method). Try rendering this component inside of ' +98'a new top-level component which will hold the ref.'99);100// Check that `component` is still the current ref because we do not want to101// detach the ref if another component stole it.102if (owner.refs[ref] === component) {103owner.detachRef(ref);104}105},106107/**108* A ReactComponent must mix this in to have refs.109*110* @lends {ReactOwner.prototype}111*/112Mixin: {113114construct: function() {115this.refs = emptyObject;116},117118/**119* Lazily allocates the refs object and stores `component` as `ref`.120*121* @param {string} ref Reference name.122* @param {component} component Component to store as `ref`.123* @final124* @private125*/126attachRef: function(ref, component) {127invariant(128component.isOwnedBy(this),129'attachRef(%s, ...): Only a component\'s owner can store a ref to it.',130ref131);132var refs = this.refs === emptyObject ? (this.refs = {}) : this.refs;133refs[ref] = component;134},135136/**137* Detaches a reference name.138*139* @param {string} ref Name to dereference.140* @final141* @private142*/143detachRef: function(ref) {144delete this.refs[ref];145}146147}148149};150151module.exports = ReactOwner;152153154