ez-saga 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,145 @@
|
|
|
1
|
-
The dy-saga project is a project that imitates dva-js
|
|
1
|
+
The dy-saga project is a project that imitates dva-js
|
|
2
|
+
|
|
3
|
+
# how to use?
|
|
4
|
+
install
|
|
5
|
+
```
|
|
6
|
+
npm install ez-saga --save
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
This is the entry file of the project.
|
|
10
|
+
```js
|
|
11
|
+
import 'core-js';
|
|
12
|
+
import 'react';
|
|
13
|
+
import React from 'react';
|
|
14
|
+
import ReactDom from 'react-dom';
|
|
15
|
+
import { BrowserRouter } from 'react-router-dom';
|
|
16
|
+
import { Provider } from 'react-redux';
|
|
17
|
+
import ezSaga from 'ez-saga';
|
|
18
|
+
import Views from './views';
|
|
19
|
+
let app = ezSaga.createApp();
|
|
20
|
+
window.app = app;
|
|
21
|
+
class RouterConfig extends React.Component {
|
|
22
|
+
render() {
|
|
23
|
+
return (
|
|
24
|
+
<Provider store={app.store}>
|
|
25
|
+
<BrowserRouter>
|
|
26
|
+
<Views app={app} />
|
|
27
|
+
</BrowserRouter >
|
|
28
|
+
</Provider>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
ReactDom.render(<RouterConfig />, document.getElementById('root'));
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
"Now let's define a model."
|
|
36
|
+
userModel.js
|
|
37
|
+
```js
|
|
38
|
+
export const modelName = 'user';
|
|
39
|
+
|
|
40
|
+
const model = {
|
|
41
|
+
/** model name */
|
|
42
|
+
name: modelName,
|
|
43
|
+
/** default state*/
|
|
44
|
+
state: {
|
|
45
|
+
userDetial: null
|
|
46
|
+
},
|
|
47
|
+
reducers: {
|
|
48
|
+
/** save user reducer */
|
|
49
|
+
saveUser: (state, action) => {
|
|
50
|
+
let newStat = {
|
|
51
|
+
...state
|
|
52
|
+
};
|
|
53
|
+
newStat.userDetial = action.payload.userDetial;
|
|
54
|
+
return newStat;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
effects: {
|
|
58
|
+
/** getUserDetial effect */
|
|
59
|
+
* getUserDetial({ payload }, { call, put, select }) {
|
|
60
|
+
//let user = payload;
|
|
61
|
+
let user = {
|
|
62
|
+
id: '1',
|
|
63
|
+
name: 'tom'
|
|
64
|
+
};
|
|
65
|
+
//async method call
|
|
66
|
+
//yield call('async', agr1, agr2, arg3, ....);
|
|
67
|
+
|
|
68
|
+
//save user
|
|
69
|
+
yield put({
|
|
70
|
+
type: `${modelName}/saveUser`,
|
|
71
|
+
payload: {
|
|
72
|
+
user: user
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
/** deleteUserDetial effect */
|
|
77
|
+
*deleteUserDetial({ payload }, { call, put, select }) {
|
|
78
|
+
let id = payload;
|
|
79
|
+
//The saveState effect is built-in by default.
|
|
80
|
+
yield put({
|
|
81
|
+
type: `${modelName}/saveState`,
|
|
82
|
+
payload: {
|
|
83
|
+
userDetial: null
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export default model;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Now we can register this model.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
import userModel from './userModel';
|
|
97
|
+
window.app.regist(userModel);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Next, we define a page.
|
|
101
|
+
```js
|
|
102
|
+
import React from 'react';
|
|
103
|
+
import { modelName } from './userModel';
|
|
104
|
+
import { connect } from 'react-redux';
|
|
105
|
+
|
|
106
|
+
class View extends React.Component {
|
|
107
|
+
|
|
108
|
+
constructor(props) {
|
|
109
|
+
super(props);
|
|
110
|
+
this.handleDelete = this.handleDelete.bind(this);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
handleDelete() {
|
|
114
|
+
this.props.dispatch({
|
|
115
|
+
type: `${modelName}/deleteUserDetial`, payload: {}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
componentDidMount() {
|
|
120
|
+
this.props.dispatch({ type: `${modelName}/getUserDetial`, payload: { id: 1 } });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
render() {
|
|
124
|
+
return (
|
|
125
|
+
<div>
|
|
126
|
+
<div>userDetail:{JSON.stringify(this.props.userDetail)}</div>
|
|
127
|
+
<button onClick={this.handleDelete}>
|
|
128
|
+
delete
|
|
129
|
+
</button>
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function stateMapProps(state, props) {
|
|
136
|
+
let model = state[modelName];
|
|
137
|
+
return {
|
|
138
|
+
userDetail: model.userDetail
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export default connect(stateMapProps)(View);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
We can invoke reducers and effects using dispatch, and we can obtain the results returned by effects.
|