From 526ca9d8c8cc561f9d9a84a2b4ef10a43d84e862 Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Fri, 24 Oct 2014 00:35:25 +0800 Subject: [PATCH] feat(GoogleMapsMixin): create mixin for context usage --- src/GoogleMap.js | 56 ------------------------------------------ src/GoogleMapsMixin.js | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 src/GoogleMap.js create mode 100644 src/GoogleMapsMixin.js diff --git a/src/GoogleMap.js b/src/GoogleMap.js deleted file mode 100644 index 9091394b..00000000 --- a/src/GoogleMap.js +++ /dev/null @@ -1,56 +0,0 @@ -/** @jsx React.DOM */ -"use strict"; -var React = require("react/addons"); - -var Map = require("./Map"); - -module.exports = React.createClass({ - displayName: "GoogleMap", - - getDefaultProps () { - return { - mapOptions: {} - }; - }, - - getInitialState () { - return { - googleMapsApi: this.props.googleMapsApi || mapsNullObject, - map: null - }; - }, - - componentWillReceiveProps (nextProps) { - var {googleMapsApi} = nextProps; - - if (mapsNullObject !== googleMapsApi) { - this.setState({ - googleMapsApi - }); - } - }, - - componentWillUnmount () { - var {googleMapsApi, map} = this.state; - this.refs.map.clear_listeners(googleMapsApi, map); - }, - - render () { - return this._render(this.props, this.state); - }, - - _on_map_ceated (map) { - this.setState({ map }); - }, - - _render (props, state) { - return
- - - {props.children} -
; - } -}); diff --git a/src/GoogleMapsMixin.js b/src/GoogleMapsMixin.js new file mode 100644 index 00000000..c9b32f65 --- /dev/null +++ b/src/GoogleMapsMixin.js @@ -0,0 +1,52 @@ +/** @jsx React.DOM */ +"use strict"; +var React = require("react/addons"); + +module.exports = { + + getInitialState () { + return { + googleMapsApi: this.props.googleMapsApi, + map: null + }; + }, + + childContextTypes: { + getMap: React.PropTypes.func, + getApi: React.PropTypes.func, + hasMap: React.PropTypes.func, + _set_map: React.PropTypes.func + }, + + contextTypes: { + getMap: React.PropTypes.func, + getApi: React.PropTypes.func, + hasMap: React.PropTypes.func, + _set_map: React.PropTypes.func + }, + + getChildContext () { + return { + getMap: this._get_map, + getApi: this._get_api, + hasMap: this._has_map, + _set_map: this._set_map + }; + }, + + _get_map () { + return this.state.map; + }, + + _get_api () { + return this.state.googleMapsApi; + }, + + _has_map () { + return !!this._get_map(); + }, + + _set_map (map) { + this.setState({ map }); + } +};