Skip to content

Instantly share code, notes, and snippets.

@karlmikko
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlmikko/07dec76a7bed23f79bdb to your computer and use it in GitHub Desktop.
Save karlmikko/07dec76a7bed23f79bdb to your computer and use it in GitHub Desktop.
GetStoresMixin
function findRoutesComponent(component){
if(component._isRoutesComponent && component._isRoutesComponent()){
return component;
}
if(component._owner){
return findRoutesComponent(component._owner);
}
return null;
};
var GetStoresMixin = {
getStores: function(){
var routesComponent = findRoutesComponent(this);
if(routesComponent && routesComponent._getStores){
return routesComponent.getStores();
}
return null;
};
module.exports = GetStoresMixin;
var Routes = React.createClass({
mixins: [GetStoresMixin],
_URLStore: null,
_RouteStore: null,
_ActiveStore: null,
_isRoutesComponent:function(){
return true;
},
_getStores:function(){
this._URLStore = this._URLStore || new URLStore();
this._RouteStore = this._RouteStore || new RouteStore();
this._ActiveStore = this._ActiveStore || new ActiveStore();
return {
URLStore: this._URLStore,
RouteStore: this._RouteStore,
ActiveStore: this._ActiveStore
};
}
});
@karlmikko
Copy link
Author

The gist allows a component to look up the this._owner tree of React to find the <Routes /> instance it is a child of. Then get all the store instances from that <Routes /> instance.

This allows the <Routes /> instance to hold the reference to all the store instances. When the <Routes /> instance is destroyed / out of scope; all the stores and listeners should be freed; pending they aren't leaked elsewhere. If the store instances are kept inside react component instances that are children of the parent <Routes /> instance, it should be fine. (Please someone tell me if this is wrong)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment