Actions contain no functionality, but rather describe an event in our application. In this lesson we will describe our Actions as they relate to our application. These Actions will then be available for triggering in our Views and for execution in our Store.

First create an list of actions in js/constants/app-constants.js

module.exports = {
ADD_ITEM: 'ADD_ITEM',
REMOVE_ITEM: 'REMOVE_ITEM',
INCREASE_ITEM: 'INCREASE_ITEM',
DECREASE_ITEM: 'DECREASE_ITEM'
};

js/actions/app-actions:

var AppConstants = require('../constants/app-constants');
var AppDispatcher = require('../dispatchers/app-dispatcher'); var AppActions = {
addItem: function (item) {
AppDispatcher.handleViewAction({
actionType: AppConstants.ADD_ITEM,
item: item
});
},
removeItem: function (index) {
AppDispatcher.handleViewAction({
actionType: AppConstants.REMOVE_ITEM,
index: index
});
},
increaseItem: function (index) {
AppDispatcher.handleViewAction({
actionType: AppConstants.INCREASE_ITEM,
index: index
});
},
descreaseItem: function (index) {
AppDispatcher.handleViewAction({
actionType: AppConstants.DECREASE_ITEM,
index: index
});
},
}; module.exports = AppActions;

For testing and see how it work, we using it for testing:

In app.js, include the actions module and bind to an click event.

var React = require('react');
var Action = require('../actions/app-actions'); var App = React.createClass({
handleClick: function () {
Action.addItem('This is an action to add');
},
render: function(){
return (<h1 onClick={this.handleClick}> My App</h1>);
}
}); module.exports = App;

Then log out the result in actions file:

    addItem: function (item) {
console.log(item );
AppDispatcher.handleViewAction({
actionType: AppConstants.ADD_ITEM,
item: item
});
},

In the console, you will see it logs out "This is an action".

[Flux] 3. Actions的更多相关文章

  1. 构建具有用户身份认证的 React + Flux 应用程序

    原文:Build a React + Flux App with User Authentication 译者:nzbin 译者的话:这是一篇内容详实的 React + Flux 教程,文章主要介绍了 ...

  2. React容器组件和展示组件

    Presentational and Container Components   展示组件   - 只关心它们的样子.   - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式.   ...

  3. Flux 普及读本

    话说当时做 APP 时,三月不知肉味,再次将眼光投放前端,有种天上一天,地下一年的感觉. Flux 是一种思想 了解的最好方式当然是看Flux官方文档了.React 中文站点也能找到对应的翻译版本,但 ...

  4. redux+flux(一:入门篇)

    React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Faceb ...

  5. 【原】flux学习笔记

    最近React(web/native)依旧如火如荼,相信大家都跃跃欲试,入职新公司,现在的团队也开始在React领域有所尝试. 2016年应该是React 逐渐走向成熟的一年.之前在原来公司搞不懂的问 ...

  6. 【go】脑补框架 Express beego tornado Flux reFlux React jsx jpg-ios出品

    http://goexpresstravel.com/ 今天 Express 的作者 TJ Holowaychuk 发了一篇文章,正式宣告和 Node.js 拜拜了,转向 Go 语言. Go vers ...

  7. [Flux] 2. Overview and Dispatchers

    Flux has four major components: Stores, Dispatchers, Views, and Actions. These components interact l ...

  8. Flux

    Ken Wheeler 构建React 应用的一套架构.  应用程序架构, 单向数据流方案. Dispatcher 的开源库.   一种全局pub/sub 系统的事件处理器, 用于 向所注册的加调函数 ...

  9. 使用 React 和 Flux 创建一个记事本应用

    React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...

随机推荐

  1. JavaScript OOP 创建对象的7种方式

    我写JS代码,可以说一直都是面向过程的写法,除了一些用来封装数据的对象或者jQuery插件,可以说对原生对象了解的是少之又少.所以我拿着<JavaScript高级程序设计 第3版>恶补了一 ...

  2. swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?

    在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...

  3. PHP扩展Redis编译安装

    PHP扩展Redis编译安装 1.下载PHP官方Redis源码包  wget http://pecl.php.net/get/redis-2.2.4.tgz  注:我用的是Redhat系统,ubunt ...

  4. max os 安装python模块PIL

    下载libjpeg和zlib: http://www.ijg.org/files/jpegsrc.v9.tar.gz http://zlib.net/zlib-1.2.8.tar.gz 安装libjp ...

  5. MDK —— configuration wizard

    学习RTX 的时候发现RTX的配置文件可以MDK的图形界面来配置,感觉这个非常好,直观.方便.健壮,可以避免程序员写入错误的数据.   参考: µVision User's Guide->Uti ...

  6. void (*f(int, void (*)(int)))(int) 函数解析 转

    今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...

  7. java怎么连接sql server,需要注意的几点

    一.JAVA连接SQL的语句    JAVA连接SQL2000语句为:  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  Drive ...

  8. java简单字符串处理

    在实际的开发工作中,对字符串的处理是最常见的编程任务. 本题目即是要求程序对用户输入的串进行处理.具体规则如下: 1. 把每个单词的首字母变为大写. 2. 把数字与字母之间用下划线字符(_)分开,使得 ...

  9. Contest 20141027 总结

    这次考试主要问题出在第一题,由于考试期间没有看清题意,少看了一句 “a=A/1e9" 导致在考试结束最后5分钟发现时修改过于匆忙,改出问题了.另外,这道题同时告诉我long double 在 ...

  10. unity 基础之InputManager

    unity  基础之InputManager 说一下unity中的InputManager,先截个图 其中Axes指的是有几个轴向!Size指的是有几个轴,改变Size可以添加或者减少轴! Name指 ...