-
-
Save johndavedecano/e668c7c44a0f74b870294de1fbad79e1 to your computer and use it in GitHub Desktop.
Global event system for React.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventSystem = (function() { | |
var self = this; | |
self.queue = {}; | |
return { | |
publish: function (event, data) { | |
var queue = self.queue[event]; | |
if (typeof queue === 'undefined') { | |
return false; | |
} | |
while(queue.length > 0) { | |
(queue.shift())(data); | |
} | |
return true; | |
}, | |
subscribe: function(event, callback) { | |
if (typeof self.queue[event] === 'undefined') { | |
self.queue[event] = []; | |
} | |
self.queue[event].push(callback); | |
} | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RiskList = React.createClass({ | |
getInitialState: function() { | |
return { risks: [] }; | |
}, | |
componentDidMount: function() { | |
// someting | |
}, | |
render: function() { | |
EventSystem.publish('risk.count.update', this.state.risks.length); | |
// something | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RiskPage = React.createClass({ | |
updateRiskCount: function(count) { | |
this.setState({ | |
riskCount: count | |
}); | |
}, | |
componentDidMount: function() { | |
EventSystem.subscribe('risk.count.update', this.updateRiskCount); | |
}, | |
getInitialState: function() { | |
return { | |
riskCount: 0 | |
}; | |
}, | |
render: function() { | |
// something | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment