[Angular 2] ngrx/store

@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJS. The result is a tool and philosophy that will transform the way you approach state management in your Angular 2 applications. This lesson takes an existing Angular 2 app and refactors it to utilize @ngrx/store, touching on all of the major concepts along the way!

 

Link: https://github.com/btroncone/ngrx-store-in-ten

 

The approach I took is a little bit different from the one shown in Github.

 

People reducer:

For perople reducer, mainly three things:

  • Add person
  • Remove person
  • Toggle person state
复制代码
export const people = (state = defaultPeople, {type, payload}) => {
    switch(type){
        case TOGGLE_STATE:
            return state.map( (person) => {
                if(person.name === payload.name){
                    let state = person.state ? !person.state : true;
                    return Object.assign({}, person, {state});
                }
                return person;
            });
        case ADD_PERSON:
            return [
                ...state,
                {name: payload, time: new Date()}
            ];
        case DELETE_PERSON:
            var index = state.indexOf(payload);
            console.log(index);
            return [
                ...state.slice(0, index),
                ...state.slice(index+1),
            ];
        default:
            return state;
    }
};
复制代码

 

Then on the interface, add input and buttons:

复制代码
        <ul>
            <li [style.textDecoration]="person.state ? 'line-through': 'none'" (click)="person$.next(person)" *ngFor="#person of people | async">
                {{person.name}} is in {{person.time | date : 'jms'}} 
                <button (click)="deletePerson$.next(person)">Remove</button>
                <button (click)="toggleState(person)">Toggle</button>
            </li>
        </ul>
        <br>
        <input type="text" #personInp><button (click)="addPerson$.next(personInp.value); personInp.value=''">Add</button>
复制代码

 

Add Person:

    addPerson$ = new Subject()
        .map( (person) => ({type: ADD_PERSON, payload: person}));

Here we create an Action: {type: ADD_PERSON, payload: person}.

Dispatch the action:

        Observable.merge(
...
this.addPerson$,
... ) .subscribe(store.dispatch.bind(store))

 

Toggle Person:

For add person, we use Subject() to emit the event. For toggle person, we just use normal function to dispatch the action:

    toggleState(person){
        this.store.dispatch({type: TOGGLE_STATE, payload: person})
    }

 

Filter:

Filter reducer add function which will be passed into the Array.filter() function:

复制代码
export const filter = (state = person => person, {type, payload}: {type: ""}) => {
    switch(type){
        case SHOW_ALL:
            return person => person;
        case SHOW_AVAILABLE:
            return person => !person.state;
        case SHOW_BUSY:
            return person => person.state;
        default:
            return state;
    }
}
复制代码

Tamplete:

        <button (click)="all$.next()">Show All</button>
        <button (click)="available$.next()">Show Available</button>
        <button (click)="busy$.next()">Show Busy</button>

Use Subject:

    all$ = new Subject()
        .mapTo({type: SHOW_ALL});

    available$ = new Subject()
        .mapTo({type: SHOW_AVAILABLE});

    busy$ = new Subject()
        .mapTo({type: SHOW_BUSY});

Dispatch:

复制代码
        Observable.merge(
            this.person$,
            this.addPerson$,
            this.deletePerson$,
            this.available$,
            this.all$,
            this.busy$
            )
            .subscribe(store.dispatch.bind(store))
复制代码

Update store:

        this.people = Observable.combineLatest(
            this.people,
            this.filter,
            ( people, filter) => {
                return people.filter(filter);
            }
        );

 

-------------------------

 

 

 

posted @   Zhentiw  阅读(633)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-05-01 [ES6] 23. Rest Parameters & Spread Parameters
点击右上角即可分享
微信分享提示