altJS是基於Flux使用Javascript應用來管理數據的類庫,它簡化了flux的store、actions、dispatcher。html
關於Flux,如下連接都作了很好的詮釋react
http://news.cnblogs.com/n/208305/web
http://www.cnblogs.com/linerz/p/react-flux-example-todomvc-analysis.htmlnpm
React, CommonJS, ES5 Javascript, 至於ES6,本文采用ES5作例,有興趣的朋友能夠轉成ES6,關於ES6瀏覽器兼容問題,還要下載babel進行轉換。api
alt是基於Note.js開發的,因此安裝前須要安裝note.js瀏覽器
使用npm安裝alt:npm install alt babel
項目結構mvc
|--actions/app
| |--MyActions.jsdom
|--stores/
| |--MyStore.js
|--components/
| |--MyComponent.jsx
|--alt.js
|--app.js
var Alt = require(‘alt’); var flux = new Alt(); module.exports = flux;
alt 經過自定義的方法進行actions的建立createActions
var flux = require(‘…/flux’); module.exports = flux.createActions({ GetData:function(input){ /*
webapi get 獲取數據data
*/ this.dispatch(data); }, GetDetail: function(id){ /*獲取單條數據*/ this.dispatch(data); } });
var flux = require(‘…/flux’); var MyActions= require(‘../actions/MyActions’); var MyStore = flux.createStore({ bindListeners:{ Handledata: MyActions.GetData, HandleDetail:MyActions.GetDetail }, Handledata:function (data){ this.setState({datas: data}); },
HandleDetail:function(data){
this.setState({data:data});
} },’MyStore’); module.exports = MyStore;
var React = require(‘react’); var MyStore = require(‘../Stores/MyStore’); var MyAction = require(‘../Stores/MyAction’); var MyComponent = React.createClass({ getInitialState:function(){ return MyStore.getState(); }, getDetail: function(data,e){ var id = data.Id; MyAction.GetDetail (id); }, componentDidMount: function(){ MyStore.listen(this.onChange); }, componentWillMount: function(){ MyStore.unlisten(this.onChange); }, onChange: function(state){ this.setState(state); }, render: function(){ return(
<ul>
{this.state.datas.map(function(data){ Return (
<li onClick={this.getDetail.bind(null,data)}>{data.name}</li>); })} </ul> ); } }); module.exports = MyComponent;