Background
In this approach you have only model with all the logic (getting data from server, converting it into client view, action creators, reducers etc …) and standard redux reducer for linking your model with reducer:
Goals
- Move all logic (action creator, api call, reducer, data transformations server-client-server, validation) in one file - MODEL
- Write once used everywhere. It's OOP style - when you base models and sub models. Can all advantages from OOP.
- Reduce code, reduce dynamic objects calling
- No dependencies
- Cross-platform - can be used in React-Native
Installation
Using npm:
$ npm install --save redux-model-s
Using yarn:
$ yarn add redux-model-s
Usage
/** * pokemon.model.js */;;; static MODEL_NAME = 'pokemonsModel'; static TYPE_REQUEST = 'REQUEST_POKEMONS'; static TYPE_FROM_SERVER = 'GET_POKEMONS'; static TYPE_CLEAR = 'CLEAR_POKEMONS'; { superprops; }; static { const limit offset = this; const url = urlspokeball; const queryParams = offset !== undefined ? limit offset: offset + limit : limit ; return ; }; static { const props = super; return ...props items: isFirstLoading: false paginator: {} hasMore: false ; }; static { if !serverModel return this; const meta objects = serverModel; const paginator = this; const items = objects ; return items paginator ; }; static { const paginator = limit: serverModellimit next: serverModelnext offset: serverModeloffset previous: serverModelprevious total_count: serverModeltotal_count ; return paginator; }; static { if return {}; return limit: paramslimit offset: paramsoffset ; } static { const modelClient model = actionpayload; const items: _items = stateModel; const items paginator = modelClient; return modelMODEL_NAME: items: ..._items ...items paginator hasMore: paginatoroffset < paginatortotal_count isLoading: false isFirstLoading: false ; }; static { const model = actionpayload; const newState = supermodelMODEL_NAME; const items isLoading = newState; return modelMODEL_NAME: ...stateModel isFirstLoading: isLoading && !items || !itemslength ; }; ;
Advanced Usage
You can fill reducers with any model
/** * pokemon.reducer.js */;;; const DEFAULT_STATE = pokemonModelMODEL_NAME: pokemonModel pokemonsModelMODEL_NAME: pokemonsModel; { return ;};
Examples
The source includes example that should help you get started.
Questions?
If you have any questions, please submit an Issue with the "question"