Last active
August 29, 2015 14:17
-
-
Save AllenFang/5826b20b443c8b78ecd7 to your computer and use it in GitHub Desktop.
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 React = require("react"); | |
var DemoApp = require("./components/DemoApp.react"); | |
React.render( | |
<DemoApp />, | |
document.getElementById("example") | |
); |
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 React = require("react"); | |
var Content = React.createClass({ | |
render: function(){ | |
return( | |
<div>{this.props.value}</div> | |
) | |
} | |
}); | |
module.exports = Content; |
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 DemoDispatcher = require("../dispatcher/DemoDispatcher"); | |
var DemoAction = { | |
increase: function(){ | |
DemoDispatcher.dispatch({ | |
actionType: "Increase" | |
}); | |
}, | |
decrease: function(){ | |
DemoDispatcher.dispatch({ | |
actionType: "Decrease" | |
}); | |
} | |
}; | |
module.exports = DemoAction; |
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 React = require("react"); | |
var Header = require("./Header.react"); | |
var Content = require("./Content.react"); | |
var DemoStore = require("../stores/DemoStore"); | |
var DemoApp = React.createClass({ | |
//initial stage, use Store to get default value and bind to the React state | |
getInitialState: function(){ | |
return{ | |
value: DemoStore.getValue() | |
}; | |
}, | |
componentDidMount: function() { | |
//listen up an event from Store, if value in Store changed, _onChange function will be triggered | |
DemoStore.addChangeListener(this._onChange); | |
}, | |
componentWillUnmount: function() { | |
DemoStore.removeChangeListener(this._onChange); | |
}, | |
render: function(){ | |
return( | |
<div> | |
<Header /> | |
<Content value={this.state.value}/> | |
</div> | |
); | |
}, | |
// If value in Store change, this function will be triggered | |
// so we need to reset the state | |
_onChange: function(){ | |
this.setState({ | |
value: DemoStore.getValue() | |
}); | |
} | |
}); | |
module.exports = DemoApp; |
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 Dispatcher = require('flux').Dispatcher; | |
module.exports = new Dispatcher(); |
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 DemoDispatcher = require('../dispatcher/DemoDispatcher'); | |
var EventEmitter = require('events').EventEmitter; | |
var assign = require("object-assign"); | |
var CHANGE_EVENT = 'change'; | |
// Our value show on the view | |
var _value = 0; | |
// increase logic | |
function _increase(){ | |
_value++; | |
} | |
// decrease logic | |
function _decrease(){ | |
_value--; | |
} | |
// define a Store object the extends EventEmitter from node.js event lib | |
var DemoStore = assign({}, EventEmitter.prototype, { | |
getValue: function(){ | |
return _value; | |
}, | |
// trigger a value changed event!! | |
emitChange: function() { | |
this.emit(CHANGE_EVENT); | |
}, | |
// the callback function will be defined and be passed from view | |
// in our example, the callback function be defined in DemoApp.react.js and named _onChange | |
addChangeListener: function(callback) { | |
this.on(CHANGE_EVENT, callback); | |
}, | |
removeChangeListener: function(callback) { | |
this.removeListener(CHANGE_EVENT, callback); | |
} | |
}); | |
//Use dispatcher to listen some events | |
DemoDispatcher.register(function(action){ | |
switch(action.actionType){ | |
case "Increase": | |
_increase(); // do increase logic | |
DemoStore.emitChange(); // after value change, trigger a event | |
break; | |
case "Decrease": | |
_decrease(); // do decrease logic | |
DemoStore.emitChange(); // after value change, trigger a event | |
break; | |
default: | |
} | |
}); | |
module.exports = DemoStore; |
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 React = require("react"); | |
var DemoAction = require("../actions/DemoAction"); | |
var Header = React.createClass({ | |
//handle click Increase button, call DemoAction's increase function | |
handleIncrease: function(){ | |
DemoAction.increase(); | |
}, | |
//handle click Decrease button, call DemoAction's decrease function | |
handleDecrease: function(){ | |
DemoAction.decrease(); | |
}, | |
render: function(){ | |
return( | |
<div> | |
<section> | |
<button onClick={this.handleIncrease}>Increase</button> | |
<button onClick={this.handleDecrease}>Decrease</button> | |
</section> | |
</div> | |
); | |
} | |
}); | |
module.exports = Header; |
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>React demo with flux</title> | |
</head> | |
<body> | |
<div id="example"></div> | |
</body> | |
</html> |
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>React demo with flux</title> | |
</head> | |
<body> | |
<div id="example"></div> | |
<script src="build/bundle.js"></script> | |
</body> | |
</html> |
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
$ npm install browserify -g | |
$ npm install browserify --save-dev | |
$ npm install reactify --save-dev |
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
{ | |
"name": "react-flux-demo", | |
"version": "1.0.0", | |
"description": "It's a simple demo for react and flux." | |
} |
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
"browserify": { | |
"transform": [ | |
"reactify" | |
] | |
} |
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
$ npm install react --save | |
$ npm install flux --save | |
$ npm install object-assign --save #object-assign > https://github.com/sindresorhus/object-assign |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment