react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / ReactStateSetters.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 ReactStateSetters9*/1011"use strict";1213var ReactStateSetters = {14/**15* Returns a function that calls the provided function, and uses the result16* of that to set the component's state.17*18* @param {ReactCompositeComponent} component19* @param {function} funcReturningState Returned callback uses this to20* determine how to update state.21* @return {function} callback that when invoked uses funcReturningState to22* determined the object literal to setState.23*/24createStateSetter: function(component, funcReturningState) {25return function(a, b, c, d, e, f) {26var partialState = funcReturningState.call(component, a, b, c, d, e, f);27if (partialState) {28component.setState(partialState);29}30};31},3233/**34* Returns a single-argument callback that can be used to update a single35* key in the component's state.36*37* Note: this is memoized function, which makes it inexpensive to call.38*39* @param {ReactCompositeComponent} component40* @param {string} key The key in the state that you should update.41* @return {function} callback of 1 argument which calls setState() with42* the provided keyName and callback argument.43*/44createStateKeySetter: function(component, key) {45// Memoize the setters.46var cache = component.__keySetters || (component.__keySetters = {});47return cache[key] || (cache[key] = createStateKeySetter(component, key));48}49};5051function createStateKeySetter(component, key) {52// Partial state is allocated outside of the function closure so it can be53// reused with every call, avoiding memory allocation when this function54// is called.55var partialState = {};56return function stateKeySetter(value) {57partialState[key] = value;58component.setState(partialState);59};60}6162ReactStateSetters.Mixin = {63/**64* Returns a function that calls the provided function, and uses the result65* of that to set the component's state.66*67* For example, these statements are equivalent:68*69* this.setState({x: 1});70* this.createStateSetter(function(xValue) {71* return {x: xValue};72* })(1);73*74* @param {function} funcReturningState Returned callback uses this to75* determine how to update state.76* @return {function} callback that when invoked uses funcReturningState to77* determined the object literal to setState.78*/79createStateSetter: function(funcReturningState) {80return ReactStateSetters.createStateSetter(this, funcReturningState);81},8283/**84* Returns a single-argument callback that can be used to update a single85* key in the component's state.86*87* For example, these statements are equivalent:88*89* this.setState({x: 1});90* this.createStateKeySetter('x')(1);91*92* Note: this is memoized function, which makes it inexpensive to call.93*94* @param {string} key The key in the state that you should update.95* @return {function} callback of 1 argument which calls setState() with96* the provided keyName and callback argument.97*/98createStateKeySetter: function(key) {99return ReactStateSetters.createStateKeySetter(this, key);100}101};102103module.exports = ReactStateSetters;104105106