Store, in Flux which manager the state of the application.

You can use EventEmiiter to listen to the change to state.

  1. import {dispatch, register} from '../dispatchers/app-dispatcher';
  2. import AppConstants from '../constants/app-constants';
  3. import { EventEmitter } from 'events';
  4.  
  5. const CHANGE_EVENT = 'change'
  6.  
  7. /**
  8. * Init the data: Start
  9. */
  10. var _catalog = [];
  11.  
  12. for ( let i = 1; i < 9; i++ ) {
  13. _catalog.push( {
  14. 'id': 'Widget' + i,
  15. 'title': 'Widget #' + i,
  16. 'summary': 'A great widget',
  17. 'description': 'Lorem ipsum dolor sit amet.',
  18. 'cost': i
  19. } );
  20. }
  21.  
  22. /**
  23. * Init the data: End
  24. */
  25.  
  26. /**
  27. * Manager the CRUD: Start
  28. */
  29. var _cartItems = [];
  30.  
  31. const _removeItem = ( item ) => {
  32. _cartItems.splice( _cartItems.findIndex( i => i === item ), 1 );
  33. };
  34.  
  35. const _findCartItem = ( item ) => {
  36. return _cartItems.find( cartItem => cartItem.id === item.id )
  37. };
  38.  
  39. const _increaseItem = ( item ) => item.qty++;
  40.  
  41. const _decreaseItem = ( item ) => {
  42. item.qty--;
  43. if ( item.qty === 0 ) {
  44. _removeItem( item )
  45. }
  46. };
  47.  
  48. const _addItem = ( item ) => {
  49. const cartItem = _findCartItem( item );
  50. if ( !cartItem ) {
  51. _cartItems.push( Object.assign( {qty: 1}, item ) );
  52. }
  53. else {
  54. _increaseItem( cartItem );
  55. }
  56. };
  57.  
  58. const _cartTotals = ( qty = 0, total = 0 ) => {
  59. _cartItems.forEach( cartItem => {
  60. qty += cartItem.qty;
  61. total += cartItem.qty * cartItem.cost;
  62. } );
  63. return {qty, total};
  64. };
  65.  
  66. /**
  67. * Manger the CRUD: END
  68. */
  69.  
  70. /**
  71. * AppStore Class, handle the request from dispatcher
  72. * @type {*}
  73. */
  74. const AppStore = Object.assign(EventEmitter.prototype, {
  75. emitChange(){
  76. this.emit( CHANGE_EVENT )
  77. },
  78.  
  79. addChangeListener( callback ){
  80. this.on( CHANGE_EVENT, callback )
  81. },
  82.  
  83. removeChangeListener( callback ){
  84. this.removeListener( CHANGE_EVENT, callback )
  85. },
  86.  
  87. getCart(){
  88. return _cartItems;
  89. },
  90.  
  91. getCatalog(){
  92. return _catalog.map(item => {
  93. return Object.assign( {}, item, _cartItems.find( cItem => cItem.id === item.id))
  94. })
  95. },
  96.  
  97. getCartTotals(){
  98. return _cartTotals();
  99. },
  100.  
  101. dispatcherIndex: register( function( action ){
  102. switch(action.actionType){
  103. case AppConstants.ADD_ITEM:
  104. _addItem( action.item );
  105. break;
  106. case AppConstants.REMOVE_ITEM:
  107. _removeItem( action.item );
  108. break;
  109.  
  110. case AppConstants.INCREASE_ITEM:
  111. _increaseItem( action.item );
  112. break;
  113.  
  114. case AppConstants.DECREASE_ITEM:
  115. _decreaseItem( action.item );
  116. break;
  117. }
  118.  
  119. AppStore.emitChange();
  120. })
  121. });
  122.  
  123. export default AppStore

[Flux] Stores的更多相关文章

  1. Flux 普及读本

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

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

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

  3. 【原】flux学习笔记

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

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

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

  5. [Flux] 2. Overview and Dispatchers

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

  6. Flux

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

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

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

  8. Flux demo

    Flux demo Introduction flux应用架构如下图所示,本文并不是讲述怎么立即做一个酷炫的应用,而是讲述如何依照这种框架,来进行代码的组织.我们先把这个流程转述为文字:抛开与webA ...

  9. Flux是一个Facebook团队的前端开发架构

    Flux是一个Facebook团队的前端开发架构 Flux introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出.一旦稍微多了解一 ...

随机推荐

  1. 『重构--改善既有代码的设计』读书笔记----Introduce Foreign Method

    当你无法获得一个类的源代码或者没有权限去修改这个类的时候,你对于这种为你服务的类,你可能会出现需要别的需求的时候,比如一个Date类,你需要能够让他本身直接返回出他的后一天的对象,但他没有,这个时候你 ...

  2. Oracle数据库之视图与索引

    Oracle数据库之视图与索引 1. 视图简介 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改. 视图基于的表称为基表,视图是存储在数据字典里的一条SE ...

  3. springMVC整合memcached,以注解形式使用

    睡不着,深夜写点博客.闲下来有一个月了,心里多少有点…… 在北京找工作一再受阻,这个时间点也不好找 再接再厉 之前没有用过memcached,没有什么实战经验,看了一些关于memcached的博客,写 ...

  4. jQuery 自动完成文本框

    jQuery自动完成插件开源软件 http://www.oschina.net/project/tag/329/jquery-autocomplete jQuery TextExt http://te ...

  5. B题 - A+B for Input-Output Practice (I)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...

  6. Egret HTTP网络

    HTTP 请求与响应: private createGameScene():void { //HTTP 1.0 var request = new egret.HttpRequest(); reque ...

  7. NET笔试题集

    题目来源于传智播客和各大互联网,复习.重新整理贴出来. 1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 ...

  8. 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 ...

  9. 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 ...

  10. C语言基础课程 第三课 ADB(Android Debug Bridge)的使用

     由于前面已经发布过Linux的博客了 基础班将Linux基础命令就不单独发表博客了,本节课主要就是利用adb连接手机进行一个Linux基本命令的复习.而且熟悉手机的底层运作,不用界面操作照样也能 ...