The first things we need to do is create a reducer:

/**
* CONSTANT
* @type {string}
*/
export const GET_CATEGORIES = "GET_CATEGORIES"; /**
* INIT VALUE
*/
export const initialCategories = [
{id: , name: 'Development'},
{id: , name: 'Design'},
{id: , name: 'Exercise'},
{id: , name: 'Humor'}
]; /**
* REDUCERS
* @type {string}
*/
export const categories = (state = initialCategories, {type, payload}) => {
switch(type) {
case GET_CATEGORIES:
return payload || state;
default:
return state;
}
};

It has some default initialize data. What it does is just simply return the state.

Then let's create a gloable store for the app, which has two methods, getState, dispatch.  Two props: reducer, state.

class Store {
constructor(reducer, initialState) {
this.reducer = reducer;
this.state= initialState;
} getState() {
return this.state
} dispatch() {
this.state = this.reducer(this.state, action);
} }

Once we got that, we are going to init our store:

import {categories, initialCategories} from './components/categories/category.state';
import Store from './app.store';
const store = new Store(categories, initialCategories);

We passed in categoreis reudcer and the initialCategories state.

To make it available to Angular APP. we need to make it injectable:

let appModule = angular.module('app', [
CommonModule.name,
ComponentsModule.name
])
.value('store', store)

Then we can use it in our app:

class CategoriesController {
constructor(store) {
'ngInject'; angular.extend(this, {
store
});
} $onInit() {
this.store.dispatch({type: GET_CATEGORIES});
this.categories = this.store.getState();
}
}

Now we are going to simply the code a little bit, we going to make a subscribe method so that we don't need to call getState() method everytime after we dispatch an action.

You can think that the subscribe method is a just callback function which each time we dispatch an action, it will be called. And inside the callback function, we will call this.store.getState() to get the value.

class Store {
constructor(reducer, initialState) {
this.reducer = reducer;
this.state = initialState;
this.listeners = [];
} getState() {
return this.state;
} dispatch(action) {
this.state = this.reducer(this.state, action);
this.listeners.forEach((l) => l());
} subscribe(listener) {
this.listeners = [
...this.listeners,
listener
]; // return an unsubscribe function
return () => {
this.listeners = this.listeners.filter(l => l !== listener);
}
}
} export default Store;
class CategoriesController {
constructor($timeout, store) {
'ngInject'; angular.extend(this, {
$timeout,
store
});
} $onInit() {
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState();
}); this.store.dispatch({type: GET_CATEGORIES});
}
}

Currently inside the dispatch() metod, we pass in an object with type and payload. It would be better if we can manage those action in a single place. There is where Action creator comes in to play.

/**
* ACTIONS CREATOR
*/
export const CategoriesActions = () => {
const getCategoreis = (categories) => {
return {type: GET_CATEGORIES, payload: categories}
}; const getCurrentCategory = (currentCategory) => {
return {type: GET_CURRENT_CATEGORY, payload: currentCategory}
}; return {
getCategoreis,
getCurrentCategory
};
};

To make it avaiable to Angular App, we can create a factory for this:

let appModule = angular.module('app', [
CommonModule.name,
ComponentsModule.name
])
.value('store', store)
.factory('CategoriesActions', CategoriesActions)
.component('app', AppComponent)

Then we can use it inside the controller:

  constructor($timeout, store, CategoriesActions) {
'ngInject'; angular.extend(this, {
$timeout,
store,
CategoriesActions
});
}
  $onInit() {
this.unsubscribe = this.store.subscribe(() => {
this.categories = this.store.getState();
}); this.store.dispatch(this.CategoriesActions.getCategoreis());
}
  onCategorySelected(currentCategory) {
this.currentCategory = category(this.currentCategory, this.CategoriesActions.getCurrentCategory(currentCategory));
}

[AngularJS] Write a simple Redux store in AngularJS app的更多相关文章

  1. [后端人员耍前端系列]AngularJs篇:30分钟快速掌握AngularJs

    一.前言 对于前端系列,自然少不了AngularJs的介绍了.在前面文章中,我们介绍了如何使用KnockoutJs来打造一个单页面程序,后面一篇文章将介绍如何使用AngularJs的开发一个单页面应用 ...

  2. Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs 前端主体结构(五)

    目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 6 Angularjs 前端主体结构 6.1 A ...

  3. Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs名词与概念(一)

    目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 2. 前言 Angularjs开发CRUD类型的 ...

  4. 【js类库AngularJs】web前端的mvc框架angularjs之hello world

    AngularJS诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中.AngularJS有着诸多特性,最为核 ...

  5. [Redux] Store Methods: getState(), dispatch(), and subscribe()

    console.clear(); const counter = (state = 0, action) => { switch (action.type) { case 'INCREMENT' ...

  6. [Functional Programming ADT] Create a Redux Store for Use with a State ADT Based Reducer

    With a well defined demarcation point between Redux and our State ADT based model, hooking up to a R ...

  7. [Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)

    In certain situations, you care more about the final state of the redux store than you do about the ...

  8. Simple Redux

    This is a post that tries to explain the the basics of Redux. We’ll build a minimal working example ...

  9. iOS App Store上架新APP与更新APP版本

    iOS App Store上架新APP与更新APP版本 http://www.jianshu.com/p/9e8d1edca148

随机推荐

  1. Ubuntu+PyQt5+Python3.6+Qt Designer 实现可视化窗口的编辑

    一.为什么写这片博文 近期将实验室的电脑的OS换成了ubuntu,想对linux进一步的了解和使用.在使用的过程中想用python+pyqt5写一个音乐播放器和视频播放器(这也是linux的乐趣所在) ...

  2. 信号 signal sigaction补充

    目前linux中的signal()是通过sigation()函数实现的. 由signal()安装的实时信号支持排队,同样不会丢失. 先看signal 和 sigaction 的区别: 关键是 stru ...

  3. 【项目基础】容器、AOP理论篇

    一.容器(砂锅) 1.概念: 容器是应用server中位于组件和平台之间的接口集合 2.应用: 容器一般位于应用server之内,由应用server负责载入和维护.一个容器仅仅能存在于一个应用serv ...

  4. 负载均衡器&http正向代理

    透明的负载均衡器&http正向代理 * master-workers架构,http正向代理由独立的dns请求以及缓冲进程  * 使用epoll(ET)模式,採用全异步方式(双缓存,实现双向同一 ...

  5. js34

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. <meta-data>的使用

    在AndroidManifest.xml中,<meta-data>元素可以作为子元素,被包含在<activity>.<application> .<servi ...

  7. Ajax跨域:Jsonp实例--百度搜索框下拉提示

    Ajax跨域:Jsonp实例--百度搜索框下拉提示 一.总结 一句话总结:a.找好接口:b.用script标签的src引入文件(json数据):c.定义及实现上一步引入文件中的函数 1.如何找到一个网 ...

  8. call、apply、bind 区别

    1.为什么要用 call .apply? 为了 改变方法里面的属性而不去改变原来的方法 function fruits() {} fruits.prototype = { color: "r ...

  9. BZOJ1492: [NOI2007]货币兑换Cash(CDQ分治,斜率优化动态规划)

    Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券(以下 简称B券).每个持有金券的顾客都有一个自己的帐户.金券的数目可以是一个 ...

  10. python学习 第六天课后总结:

    <br class="Apple-interchange-newline"><div></div>       python学习 第六天课后总结 ...