3.6.4 • Published 5 years ago

glue-redux v3.6.4

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

build codecov NPM version NPM downloads package size license

glue-redux

基于redux的应用层

简单、友好、内聚,让相关代码在相近的位置出现


数据模型

model

模型和state的结构是一一对应的.

获取数据源state中的p3节点

referToState(model.p1.p3)

retrieve

更新数据源state中的p3节点

model.p1.p3(data)

update

红色节点代表被更新的节点


查看示例

git clone https://github.com/ZhouYK/glue-redux.git
npm install
npm start

然后访问 http://localhost:8888

API

名称用途
gluer定义可维护节点
destruct解构由可维护节点组成的普通对象

gluer(updater, initialValue) | 代码

定义可维护节点,根据入参会有不同的处理

入参

参数名类型用途示例
updater函数用于处理数据,强烈建议数据处理的情况不要有超过两种,超过了应提取出来放置别处function (data, state) {}
initialValue任意值节点初始值,表明节点的数据结构和数据类型(开发模式下要求必填)any

栗子

// 定义model
  import { gluer } from 'glue-redux';
  
  const users = gluer((data, state) => [data, ...state], []);
  const profile = {
    date: gluer(1),
  };
  
  const country = gluer('');
  
  const app = {
    users,
    country,
    profile,
  };
  export default app;

👆上面的栗子是gluer的基本用法,如果想进一步细致地控制数据可以看这里gluer的高级用法

关于gluer的入参选择,可以有多种模式

 // 不传参数
 const name = gluer(); // 等价于 const name =  gluer(data => data)
 
 // 只有一个入参,类型为函数,该函数会用于数据处理
 const name = gluer((data, state) => data.substring(1,3));
 
 // 只有一个入参,类型为非函数,该参数会被当做初始值
 const name = gluer('initialValue'); // 等价于 const name = gluer(data => data, 'initialValue')
 
 // 两个参数,第一个为数据处理函数,第二个为初始值
 const name = gluer((data, state) => { ...state, ...data }, {name: 'initialValue'})
 

destruct(store)(models)

解构数据对象,与redux进行连接

入参

参数名类型用途示例
storeredux的store供数据模型使用-
modelsobject数据模型{ index: string: GluerReturn or any}

返回

  • { reducers, actions, referToState, hasModel }

    包含reducers和actions属性的对象

属性名类型用途示例
reducersobjectreducer组成的对象{ name: (state, action) => {}, ... }
actionsobjectdispatcher组成的对象{ name: GluerReturn, ... }
referToStatefunction用于从state中索引出数据referToState(index:any)
hasModelfunction用于判断传入的内容是否被索引了hasModel(index: any)(和referToState使用一套索引原则,可查看referToState的使用)

栗子

// store.js
import {
  createStore, combineReducers,
} from 'redux';
import { destruct } from 'glue-redux';
import model from './model';

const store = createStore(() => {});
const { reducers, referToState, hasModel } = destruct(store)(model);
store.replaceReducer(combineReducers(reducers));

export {
  store,
  referToState, // 根据reference 获取store中对应的数据,如果没有则返回undefined
  hasModel, // 判断reference 是否在store中有对应的数据
};

使用model进行数据更新

// service.js
import app from './model';

const register = (data) => {
  // any operation about data
  app.users(data);
};
const service = {
  register,
};
export default service;
import { referToState } from './store';
import app from './model';
import service from './service';

service.register({
  name: '小明',
  age: 18,
  pet: '猫'
});
console.log('app model的数据为:', referToState(app));
// { users: [{name: '小明', age: 18, pet: '猫'}] }
console.log('app model中的users为:', referToState(app.users));
// [{name: '小明', age: 18, pet: '猫'}]

扩展文档

地址摘要
说明文档更加详细说明glue-redux的原理
实践说明处理异步问题以及如何组织代码
react-glux与react的连接库,HOC方式
react-glue-redux-hook与react的连接库,包含HOC和hook两种方式
设计一个前端数据模型,让数据操作更简单可靠代码结构设计说明

Author

ZhouYK

License

MIT licensed

3.6.4

5 years ago

3.6.4-alpha.0

5 years ago

3.6.3-alpha.0

5 years ago

3.6.2

5 years ago

3.6.1

5 years ago

3.6.0

5 years ago

3.6.0-alpha.0

5 years ago

3.5.6

5 years ago

3.5.5

5 years ago

3.5.4

5 years ago

3.5.3

5 years ago

3.5.2

5 years ago

3.5.1

5 years ago

3.5.0

5 years ago

3.4.3

5 years ago

3.4.2

5 years ago

3.4.1

5 years ago

3.4.0

5 years ago

3.4.0-alpha.1

5 years ago

3.4.0-alpha.0

5 years ago

3.3.0

5 years ago

3.3.0-0

5 years ago

3.2.2

5 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.6

6 years ago