Skip to content

Commit

Permalink
feat(ChildMixin): add invalid_context to check before operations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchentw committed Oct 24, 2014
1 parent 9a90337 commit 5d33c30
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ module.exports = React.createClass({
},

componentDidMount () {
if (this.invalid_context(true)) return;
this._init_map(this.context);
},

componentDidUpdate () {
if (this.invalid_context(true)) return;
this._init_map(this.context);
},

Expand All @@ -26,9 +28,6 @@ module.exports = React.createClass({
},

_init_map (context) {
if (context.hasMap() || !context.getApi()) {
return;
}
var {Map} = context.getApi();
context._set_map(
new Map(
Expand Down
6 changes: 2 additions & 4 deletions src/Marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var React = require("react/addons");

var ChildMixin = require("./mixins/ChildMixin");


module.exports = React.createClass({
displayName: "Marker",

Expand All @@ -23,10 +22,12 @@ module.exports = React.createClass({
},

componentDidMount () {
if (this.invalid_context(this.state.marker)) return;
this._init_marker(this.context);
},

componentDidUpdate () {
if (this.invalid_context(this.state.marker)) return;
this._init_marker(this.context);
},

Expand All @@ -35,9 +36,6 @@ module.exports = React.createClass({
},

_init_marker (context) {
if (this.state.marker || !context.hasMap() || !context.getApi()) {
return;
}
var {Marker} = context.getApi();
var marker = new Marker(this.props);
marker.setMap(context.getMap());
Expand Down
8 changes: 7 additions & 1 deletion src/mixins/ChildMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ var React = require("react/addons");
module.exports = {

shouldComponentUpdate(nextProps, nextState) {
return false;
return JSON.stringify(nextProps) !== JSON.stringify(this.props);
},

contextTypes: {
getMap: React.PropTypes.func,
getApi: React.PropTypes.func,
hasMap: React.PropTypes.func,
},

invalid_context (instance) {
var {context} = this;
var invalidMap = (true === instance ? context.hasMap() : (instance || !context.hasMap()));
return invalidMap || !context.getApi();
}
};

0 comments on commit 5d33c30

Please sign in to comment.