[Flux] Stores
Store, in Flux which manager the state of the application.
You can use EventEmiiter to listen to the change to state.
- import {dispatch, register} from '../dispatchers/app-dispatcher';
- import AppConstants from '../constants/app-constants';
- import { EventEmitter } from 'events';
- const CHANGE_EVENT = 'change'
- /**
- * Init the data: Start
- */
- var _catalog = [];
- for ( let i = 1; i < 9; i++ ) {
- _catalog.push( {
- 'id': 'Widget' + i,
- 'title': 'Widget #' + i,
- 'summary': 'A great widget',
- 'description': 'Lorem ipsum dolor sit amet.',
- 'cost': i
- } );
- }
- /**
- * Init the data: End
- */
- /**
- * Manager the CRUD: Start
- */
- var _cartItems = [];
- const _removeItem = ( item ) => {
- _cartItems.splice( _cartItems.findIndex( i => i === item ), 1 );
- };
- const _findCartItem = ( item ) => {
- return _cartItems.find( cartItem => cartItem.id === item.id )
- };
- const _increaseItem = ( item ) => item.qty++;
- const _decreaseItem = ( item ) => {
- item.qty--;
- if ( item.qty === 0 ) {
- _removeItem( item )
- }
- };
- const _addItem = ( item ) => {
- const cartItem = _findCartItem( item );
- if ( !cartItem ) {
- _cartItems.push( Object.assign( {qty: 1}, item ) );
- }
- else {
- _increaseItem( cartItem );
- }
- };
- const _cartTotals = ( qty = 0, total = 0 ) => {
- _cartItems.forEach( cartItem => {
- qty += cartItem.qty;
- total += cartItem.qty * cartItem.cost;
- } );
- return {qty, total};
- };
- /**
- * Manger the CRUD: END
- */
- /**
- * AppStore Class, handle the request from dispatcher
- * @type {*}
- */
- const AppStore = Object.assign(EventEmitter.prototype, {
- emitChange(){
- this.emit( CHANGE_EVENT )
- },
- addChangeListener( callback ){
- this.on( CHANGE_EVENT, callback )
- },
- removeChangeListener( callback ){
- this.removeListener( CHANGE_EVENT, callback )
- },
- getCart(){
- return _cartItems;
- },
- getCatalog(){
- return _catalog.map(item => {
- return Object.assign( {}, item, _cartItems.find( cItem => cItem.id === item.id))
- })
- },
- getCartTotals(){
- return _cartTotals();
- },
- dispatcherIndex: register( function( action ){
- switch(action.actionType){
- case AppConstants.ADD_ITEM:
- _addItem( action.item );
- break;
- case AppConstants.REMOVE_ITEM:
- _removeItem( action.item );
- break;
- case AppConstants.INCREASE_ITEM:
- _increaseItem( action.item );
- break;
- case AppConstants.DECREASE_ITEM:
- _decreaseItem( action.item );
- break;
- }
- AppStore.emitChange();
- })
- });
- export default AppStore
[Flux] Stores的更多相关文章
- Flux 普及读本
话说当时做 APP 时,三月不知肉味,再次将眼光投放前端,有种天上一天,地下一年的感觉. Flux 是一种思想 了解的最好方式当然是看Flux官方文档了.React 中文站点也能找到对应的翻译版本,但 ...
- redux+flux(一:入门篇)
React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Faceb ...
- 【原】flux学习笔记
最近React(web/native)依旧如火如荼,相信大家都跃跃欲试,入职新公司,现在的团队也开始在React领域有所尝试. 2016年应该是React 逐渐走向成熟的一年.之前在原来公司搞不懂的问 ...
- 【go】脑补框架 Express beego tornado Flux reFlux React jsx jpg-ios出品
http://goexpresstravel.com/ 今天 Express 的作者 TJ Holowaychuk 发了一篇文章,正式宣告和 Node.js 拜拜了,转向 Go 语言. Go vers ...
- [Flux] 2. Overview and Dispatchers
Flux has four major components: Stores, Dispatchers, Views, and Actions. These components interact l ...
- Flux
Ken Wheeler 构建React 应用的一套架构. 应用程序架构, 单向数据流方案. Dispatcher 的开源库. 一种全局pub/sub 系统的事件处理器, 用于 向所注册的加调函数 ...
- 使用 React 和 Flux 创建一个记事本应用
React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...
- Flux demo
Flux demo Introduction flux应用架构如下图所示,本文并不是讲述怎么立即做一个酷炫的应用,而是讲述如何依照这种框架,来进行代码的组织.我们先把这个流程转述为文字:抛开与webA ...
- Flux是一个Facebook团队的前端开发架构
Flux是一个Facebook团队的前端开发架构 Flux introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出.一旦稍微多了解一 ...
随机推荐
- 『重构--改善既有代码的设计』读书笔记----Introduce Foreign Method
当你无法获得一个类的源代码或者没有权限去修改这个类的时候,你对于这种为你服务的类,你可能会出现需要别的需求的时候,比如一个Date类,你需要能够让他本身直接返回出他的后一天的对象,但他没有,这个时候你 ...
- Oracle数据库之视图与索引
Oracle数据库之视图与索引 1. 视图简介 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改. 视图基于的表称为基表,视图是存储在数据字典里的一条SE ...
- springMVC整合memcached,以注解形式使用
睡不着,深夜写点博客.闲下来有一个月了,心里多少有点…… 在北京找工作一再受阻,这个时间点也不好找 再接再厉 之前没有用过memcached,没有什么实战经验,看了一些关于memcached的博客,写 ...
- jQuery 自动完成文本框
jQuery自动完成插件开源软件 http://www.oschina.net/project/tag/329/jquery-autocomplete jQuery TextExt http://te ...
- B题 - A+B for Input-Output Practice (I)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- Egret HTTP网络
HTTP 请求与响应: private createGameScene():void { //HTTP 1.0 var request = new egret.HttpRequest(); reque ...
- NET笔试题集
题目来源于传智播客和各大互联网,复习.重新整理贴出来. 1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 ...
- A Statistical View of Deep Learning (II): Auto-encoders and Free Energy
A Statistical View of Deep Learning (II): Auto-encoders and Free Energy With the success of discrimi ...
- Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?
Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...
- C语言基础课程 第三课 ADB(Android Debug Bridge)的使用
由于前面已经发布过Linux的博客了 基础班将Linux基础命令就不单独发表博客了,本节课主要就是利用adb连接手机进行一个Linux基本命令的复习.而且熟悉手机的底层运作,不用界面操作照样也能 ...