4
4
5
5
> 此 behavior 依赖开发者工具的 npm 构建。具体详情可查阅 [ 官方 npm 文档] ( https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html ) 。
6
6
7
- > 可配合 MobX 的小程序构建版 npm 模块 [ ` mobx-miniprogram ` ] ( https://github.com/wechat-miniprogram/mobx ) 使用。
8
-
9
7
## 使用方法
10
8
11
- 需要小程序基础库版本 >= 2.2.3 的环境。
9
+ 需要小程序基础库版本 >= 2.11.0 的环境。
12
10
13
- 也可以直接参考这个代码片段(在微信开发者工具中打开): [ https://developers.weixin.qq.com/s/nGvWJ2mL7et0 ] ( https://developers.weixin.qq.com/s/nGvWJ2mL7et0 ) 。
11
+ 具体的示例完整代码,可以参考 [ examples ] ( ./examples/ ) 。
14
12
15
13
1 . 安装 ` mobx-miniprogram ` 和 ` mobx-miniprogram-bindings ` :
16
14
@@ -22,7 +20,10 @@ npm install --save mobx-miniprogram mobx-miniprogram-bindings
22
20
23
21
``` js
24
22
// store.js
25
- import { observable , action } from " mobx-miniprogram" ;
23
+ import { observable , action } from ' mobx-miniprogram'
24
+
25
+ // 创建 store 时可以采用任何 mobx 的接口风格
26
+ // 这里以传统的 observable 风格为例
26
27
27
28
export const store = observable ({
28
29
// 数据字段
@@ -31,142 +32,134 @@ export const store = observable({
31
32
32
33
// 计算属性
33
34
get sum () {
34
- return this .numA + this .numB ;
35
+ return this .numA + this .numB
35
36
},
36
37
37
38
// actions
38
39
update: action (function () {
39
- const sum = this .sum ;
40
- this .numA = this .numB ;
41
- this .numB = sum;
40
+ const sum = this .sum
41
+ this .numA = this .numB
42
+ this .numB = sum
42
43
}),
43
- });
44
+ })
44
45
```
45
46
46
- 3 . 在 Component 构造器中使用:
47
+ 3 . 在 Page 或 Component 构造器中使用:
47
48
48
49
``` js
49
- import { storeBindingsBehavior } from " mobx-miniprogram-bindings" ;
50
- import { store } from " ./store" ;
50
+ import { storeBindingsBehavior } from ' mobx-miniprogram-bindings'
51
+ import { store } from ' ./store'
51
52
52
53
Component ({
53
- behaviors: [storeBindingsBehavior],
54
+ behaviors: [storeBindingsBehavior], // 添加这个 behavior
54
55
data: {
55
- someData: " ..." ,
56
+ someData: ' ...' ,
56
57
},
57
58
storeBindings: {
58
59
store,
59
60
fields: {
60
61
numA : () => store .numA ,
61
62
numB : (store ) => store .numB ,
62
- sum: " sum" ,
63
+ sum: ' sum' ,
63
64
},
64
65
actions: {
65
- buttonTap: " update" ,
66
+ buttonTap: ' update' ,
66
67
},
67
68
},
68
69
methods: {
69
70
myMethod () {
70
- this .data .sum ; // 来自于 MobX store 的字段
71
+ this .data .sum // 来自于 MobX store 的字段
71
72
},
72
73
},
73
- });
74
+ })
74
75
```
75
76
76
- 4 . 在 Page 构造器中使用:
77
+ ## TypeScript 接口
77
78
78
- 如果小程序基础库版本在 2.9.2 以上,可以直接像上面 Component 构造器那样引入 behaviors 。
79
+ 在 TypeScript 下,可以使用 ` ComponentWithStore ` 接口。它会自动处理一些类型问题 。
79
80
80
- 如果需要比较好的兼容性,可以使用下面这种方式(或者直接改用 Component 构造器来创建页面)。
81
+ (注意,使用这个接口时,不要在 behaviors 中额外引入 ` storeBindingsBehavior ` 。)
81
82
82
83
``` js
83
- import { createStoreBindings } from " mobx-miniprogram-bindings" ;
84
- import { store } from " ./store" ;
85
-
86
- Page ({
87
- data: {
88
- someData: " ..." ,
89
- },
90
- onLoad () {
91
- this .storeBindings = createStoreBindings (this , {
92
- store,
93
- fields: [" numA" , " numB" , " sum" ],
94
- actions: [" update" ],
95
- });
96
- },
97
- onUnload () {
98
- this .storeBindings .destroyStoreBindings ();
99
- },
100
- myMethod () {
101
- this .data .sum ; // 来自于 MobX store 的字段
102
- },
103
- });
104
- ```
105
-
106
- ## Typescript 支持
84
+ import { ComponentWithStore } from ' mobx-miniprogram-bindings'
107
85
108
- 从 ` 2.1.2 ` 版本开始提供了简单的 ` TypeScript ` 支持。
109
-
110
- 新增两个构造器 ` API ` ,推荐优先使用下列新版接口,你也可以继续使用旧版接口,详见接口说明。
111
-
112
- ### ComponentWithStore
113
-
114
- ``` js
115
- import { ComponentWithStore } from " mobx-miniprogram-binding" ;
116
86
ComponentWithStore ({
117
87
options: {
118
- styleIsolation: " shared" ,
88
+ styleIsolation: ' shared' ,
119
89
},
120
90
data: {
121
- someData: " ..." ,
91
+ someData: ' ...' ,
122
92
},
123
93
storeBindings: {
124
94
store,
125
- fields: [" numA" , " numB" , " sum" ],
95
+ fields: [' numA' , ' numB' , ' sum' ],
126
96
actions: {
127
- buttonTap: " update" ,
97
+ buttonTap: ' update' ,
128
98
},
129
99
},
130
- });
100
+ })
131
101
```
132
102
133
- ### BehaviorWithStore
103
+ ` BehaviorWithStore ` 接口类似。
134
104
135
105
``` js
136
- import { BehaviorWithStore } from " mobx-miniprogram-binding" ;
106
+ import { BehaviorWithStore } from ' mobx-miniprogram-bindings'
107
+
137
108
export const testBehavior = BehaviorWithStore ({
138
109
storeBindings: {
139
110
store,
140
- fields: [" numA" , " numB" , " sum" ],
141
- actions: [" update" ],
111
+ fields: [' numA' , ' numB' , ' sum' ],
112
+ actions: [' update' ],
142
113
},
143
- });
114
+ })
115
+ ```
116
+
117
+ ## glass-easel Chaining API 接口
118
+
119
+ 使用 glass-easel Chaining API 时,使用 ` initStoreBindings ` 更友好。
120
+
121
+ ``` js
122
+ import { initStoreBindings } from ' mobx-miniprogram-bindings'
123
+
124
+ Component ()
125
+ .init ((ctx ) => {
126
+ const { listener } = ctx
127
+ initStoreBindings (ctx, {
128
+ store,
129
+ fields: [' numA' , ' numB' , ' sum' ],
130
+ })
131
+ const buttonTap = listener (() => {
132
+ store .update ()
133
+ })
134
+ return { buttonTap }
135
+ })
136
+ .register ()
144
137
```
145
138
146
- ## 接口
139
+ ## 具体接口说明
147
140
148
141
将页面、自定义组件和 store 绑定有两种方式: ** behavior 绑定** 和 ** 手工绑定** 。
149
142
150
143
### behavior 绑定
151
144
152
145
** behavior 绑定** 适用于 ` Component ` 构造器。做法:使用 ` storeBindingsBehavior ` 这个 behavior 和 ` storeBindings ` 定义段。
153
146
154
- 注意:你可以用 ` Component ` 构造器构造页面, [ 参考文档] ( https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page.html#%E4%BD%BF%E7%94%A8-Component-%E6%9E%84%E9%80%A0%E5%99%A8%E6%9E%84%E9%80%A0%E9%A1%B5%E9%9D%A2 ) 。
155
-
156
147
``` js
157
- import { storeBindingsBehavior } from " mobx-miniprogram-bindings" ;
148
+ import { storeBindingsBehavior } from ' mobx-miniprogram-bindings'
149
+
158
150
Component ({
159
151
behaviors: [storeBindingsBehavior],
160
152
storeBindings: {
161
153
/* 绑定配置(见下文) */
162
154
},
163
- });
155
+ })
164
156
```
165
157
166
158
也可以把 ` storeBindings ` 设置为一个数组,这样可以同时绑定多个 ` store ` :
167
159
168
160
``` js
169
- import { storeBindingsBehavior } from " mobx-miniprogram-bindings" ;
161
+ import { storeBindingsBehavior } from ' mobx-miniprogram-bindings'
162
+
170
163
Component ({
171
164
behaviors: [storeBindingsBehavior],
172
165
storeBindings: [
@@ -177,28 +170,30 @@ Component({
177
170
/* 绑定配置 2 */
178
171
},
179
172
],
180
- });
173
+ })
181
174
```
182
175
183
176
### 手工绑定
184
177
185
- ** 手工绑定** 适用于全部场景。做法:使用 ` createStoreBindings ` 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。
178
+ ** 手工绑定** 更加灵活,适用于 store 需要在 ` onLoad ` (自定义组件 attached )时才能确定的情况。
179
+
180
+ 做法:使用 ` createStoreBindings ` 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。
186
181
187
182
注意:在页面 onUnload (自定义组件 detached )时一定要调用清理函数,否则将导致内存泄漏!
188
183
189
184
``` js
190
- import { createStoreBindings } from " mobx-miniprogram-bindings" ;
185
+ import { createStoreBindings } from ' mobx-miniprogram-bindings'
191
186
192
187
Page ({
193
188
onLoad () {
194
189
this .storeBindings = createStoreBindings (this , {
195
190
/* 绑定配置(见下文) */
196
- });
191
+ })
197
192
},
198
193
onUnload () {
199
- this .storeBindings .destroyStoreBindings ();
194
+ this .storeBindings .destroyStoreBindings ()
200
195
},
201
- });
196
+ })
202
197
```
203
198
204
199
### 绑定配置
@@ -251,7 +246,7 @@ Page({
251
246
Component ({
252
247
behaviors: [storeBindingsBehavior, computedBehavior],
253
248
/* ... */
254
- });
249
+ })
255
250
```
256
251
257
252
### 关于部分更新
@@ -263,19 +258,19 @@ Component({
263
258
behaviors: [storeBindingsBehavior],
264
259
storeBindings: {
265
260
store,
266
- fields: [" someObject" ],
261
+ fields: [' someObject' ],
267
262
},
268
- });
263
+ })
269
264
```
270
265
271
266
如果尝试在 ` store ` 中:
272
267
273
268
``` js
274
- this .someObject .someField = " xxx" ;
269
+ this .someObject .someField = ' xxx'
275
270
```
276
271
277
272
这样是不会触发界面更新的。请考虑改成:
278
273
279
274
``` js
280
- this .someObject = Object .assign ({}, this .someObject , { someField: " xxx" });
275
+ this .someObject = Object .assign ({}, this .someObject , { someField: ' xxx' })
281
276
```
0 commit comments