Alt.js的入门

1、什么是Alt

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

2、还须要了解

React, CommonJS, ES5 Javascript, 至于ES6,本文采用ES5作例,有兴趣的朋友能够转成ES6,关于ES6浏览器兼容问题,还要下载babel进行转换。api

3、安装

alt是基于Note.js开发的,因此安装前须要安装note.js浏览器

使用npm安装alt:npm install alt babel

4、Alt的基本结构

项目结构mvc

|--actions/app

|  |--MyActions.jsdom

|--stores/

|  |--MyStore.js

|--components/

|  |--MyComponent.jsx

|--alt.js

|--app.js

6、建立alt

var Alt = require(‘alt’);

var flux = new Alt();

module.exports = flux;

 

7、建立Actions

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);    } });

8、建立Store

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;

9、在View中使用MyComponent.jsx

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;
相关文章
相关标签/搜索