Skip to content

Commit 83982a5

Browse files
authored
Merge pull request #48 from wechat-miniprogram/feat-mobx-v6
Major update: v3
2 parents d470a06 + 63e87e1 commit 83982a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1665
-7426
lines changed

.eslintrc.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module.exports = {
22
root: true,
3-
parser: "@typescript-eslint/parser",
3+
parser: '@typescript-eslint/parser',
44
parserOptions: {
55
ecmaVersion: 9,
66
ecmaFeatures: {
77
jsx: false,
88
},
9-
sourceType: "module",
9+
sourceType: 'module',
1010
},
1111
env: {
1212
es6: true,
@@ -24,19 +24,19 @@ module.exports = {
2424
wx: true,
2525
getCurrentPages: true,
2626
},
27-
plugins: ["@typescript-eslint", "prettier"],
27+
plugins: ['@typescript-eslint', 'prettier'],
2828
extends: [
2929
'eslint:recommended',
3030
'plugin:@typescript-eslint/eslint-recommended',
3131
'plugin:@typescript-eslint/recommended',
32-
'prettier/@typescript-eslint'
32+
'prettier/@typescript-eslint',
3333
],
3434
rules: {
35-
"prettier/prettier": "error",
36-
"@typescript-eslint/ban-ts-ignore": "off",
37-
"@typescript-eslint/no-empty-function": "off",
38-
"@typescript-eslint/explicit-function-return-type": "off",
39-
"@typescript-eslint/no-explicit-any": "off",
40-
"@typescript-eslint/no-non-null-assertion": "off",
41-
}
42-
};
35+
'prettier/prettier': 'error',
36+
'@typescript-eslint/ban-ts-ignore': 'off',
37+
'@typescript-eslint/no-empty-function': 'off',
38+
'@typescript-eslint/explicit-function-return-type': 'off',
39+
'@typescript-eslint/no-explicit-any': 'off',
40+
'@typescript-eslint/no-non-null-assertion': 'off',
41+
},
42+
}

.prettierrc.js

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
11
module.exports = {
2-
// 字符串使用单引号
3-
singleQuote: false,
4-
// 每行末尾自动添加分号
5-
semi: true,
6-
// tab缩进大小,默认为2
2+
printWidth: 100,
73
tabWidth: 2,
8-
// 使用tab缩进,默认false
9-
useTabs: true,
10-
// 对象中打印空格 默认true
11-
// true: { foo: bar }
12-
// false: {foo: bar}
4+
useTabs: false,
5+
semi: false,
6+
singleQuote: true,
7+
quoteProps: 'as-needed',
8+
trailingComma: 'all',
139
bracketSpacing: true,
14-
// 箭头函数参数括号 默认avoid 可选 avoid| always
15-
// avoid 能省略括号的时候就省略 例如x => x
16-
// always 总是有括号
17-
arrowParens: 'avoid',
18-
// 换行长度,默认80
19-
printWidth: 80,
20-
21-
// 设置为true时,将多行JSX元素的 > 放在最后一行的末尾,而不是单独放在下一行
22-
/*
23-
<button
24-
className="prettier-class"
25-
id="prettier-id"
26-
onClick={this.handleClick}>
27-
Click Here
28-
</button>
29-
*/
30-
// 设置为false时
31-
/*
32-
<button
33-
className="prettier-class"
34-
id="prettier-id"
35-
onClick={this.handleClick}
36-
>
37-
Click Here
38-
</button>
39-
*/
40-
jsxBracketSameLine: true
41-
};
10+
arrowParens: 'always',
11+
requirePragma: false,
12+
insertPragma: false,
13+
proseWrap: 'preserve',
14+
endOfLine: 'lf',
15+
embeddedLanguageFormatting: 'auto',
16+
}

README.md

Lines changed: 76 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
> 此 behavior 依赖开发者工具的 npm 构建。具体详情可查阅 [官方 npm 文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)
66
7-
> 可配合 MobX 的小程序构建版 npm 模块 [`mobx-miniprogram`](https://github.com/wechat-miniprogram/mobx) 使用。
8-
97
## 使用方法
108

11-
需要小程序基础库版本 >= 2.2.3 的环境。
9+
需要小程序基础库版本 >= 2.11.0 的环境。
1210

13-
也可以直接参考这个代码片段(在微信开发者工具中打开): [https://developers.weixin.qq.com/s/nGvWJ2mL7et0](https://developers.weixin.qq.com/s/nGvWJ2mL7et0)
11+
具体的示例完整代码,可以参考 [examples](./examples/)
1412

1513
1. 安装 `mobx-miniprogram``mobx-miniprogram-bindings`
1614

@@ -22,7 +20,10 @@ npm install --save mobx-miniprogram mobx-miniprogram-bindings
2220

2321
```js
2422
// store.js
25-
import { observable, action } from "mobx-miniprogram";
23+
import { observable, action } from 'mobx-miniprogram'
24+
25+
// 创建 store 时可以采用任何 mobx 的接口风格
26+
// 这里以传统的 observable 风格为例
2627

2728
export const store = observable({
2829
// 数据字段
@@ -31,142 +32,134 @@ export const store = observable({
3132

3233
// 计算属性
3334
get sum() {
34-
return this.numA + this.numB;
35+
return this.numA + this.numB
3536
},
3637

3738
// actions
3839
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
4243
}),
43-
});
44+
})
4445
```
4546

46-
3. 在 Component 构造器中使用:
47+
3.Page 或 Component 构造器中使用:
4748

4849
```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'
5152

5253
Component({
53-
behaviors: [storeBindingsBehavior],
54+
behaviors: [storeBindingsBehavior], // 添加这个 behavior
5455
data: {
55-
someData: "...",
56+
someData: '...',
5657
},
5758
storeBindings: {
5859
store,
5960
fields: {
6061
numA: () => store.numA,
6162
numB: (store) => store.numB,
62-
sum: "sum",
63+
sum: 'sum',
6364
},
6465
actions: {
65-
buttonTap: "update",
66+
buttonTap: 'update',
6667
},
6768
},
6869
methods: {
6970
myMethod() {
70-
this.data.sum; // 来自于 MobX store 的字段
71+
this.data.sum // 来自于 MobX store 的字段
7172
},
7273
},
73-
});
74+
})
7475
```
7576

76-
4. 在 Page 构造器中使用:
77+
## TypeScript 接口
7778

78-
如果小程序基础库版本在 2.9.2 以上,可以直接像上面 Component 构造器那样引入 behaviors
79+
在 TypeScript 下,可以使用 `ComponentWithStore` 接口。它会自动处理一些类型问题
7980

80-
如果需要比较好的兼容性,可以使用下面这种方式(或者直接改用 Component 构造器来创建页面)。
81+
(注意,使用这个接口时,不要在 behaviors 中额外引入 `storeBindingsBehavior` 。)
8182

8283
```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'
10785

108-
`2.1.2` 版本开始提供了简单的 `TypeScript` 支持。
109-
110-
新增两个构造器 `API`,推荐优先使用下列新版接口,你也可以继续使用旧版接口,详见接口说明。
111-
112-
### ComponentWithStore
113-
114-
```js
115-
import { ComponentWithStore } from "mobx-miniprogram-binding";
11686
ComponentWithStore({
11787
options: {
118-
styleIsolation: "shared",
88+
styleIsolation: 'shared',
11989
},
12090
data: {
121-
someData: "...",
91+
someData: '...',
12292
},
12393
storeBindings: {
12494
store,
125-
fields: ["numA", "numB", "sum"],
95+
fields: ['numA', 'numB', 'sum'],
12696
actions: {
127-
buttonTap: "update",
97+
buttonTap: 'update',
12898
},
12999
},
130-
});
100+
})
131101
```
132102

133-
### BehaviorWithStore
103+
`BehaviorWithStore` 接口类似。
134104

135105
```js
136-
import { BehaviorWithStore } from "mobx-miniprogram-binding";
106+
import { BehaviorWithStore } from 'mobx-miniprogram-bindings'
107+
137108
export const testBehavior = BehaviorWithStore({
138109
storeBindings: {
139110
store,
140-
fields: ["numA", "numB", "sum"],
141-
actions: ["update"],
111+
fields: ['numA', 'numB', 'sum'],
112+
actions: ['update'],
142113
},
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()
144137
```
145138

146-
## 接口
139+
## 具体接口说明
147140

148141
将页面、自定义组件和 store 绑定有两种方式: **behavior 绑定****手工绑定**
149142

150143
### behavior 绑定
151144

152145
**behavior 绑定** 适用于 `Component` 构造器。做法:使用 `storeBindingsBehavior` 这个 behavior 和 `storeBindings` 定义段。
153146

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-
156147
```js
157-
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
148+
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
149+
158150
Component({
159151
behaviors: [storeBindingsBehavior],
160152
storeBindings: {
161153
/* 绑定配置(见下文) */
162154
},
163-
});
155+
})
164156
```
165157

166158
也可以把 `storeBindings` 设置为一个数组,这样可以同时绑定多个 `store`
167159

168160
```js
169-
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
161+
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
162+
170163
Component({
171164
behaviors: [storeBindingsBehavior],
172165
storeBindings: [
@@ -177,28 +170,30 @@ Component({
177170
/* 绑定配置 2 */
178171
},
179172
],
180-
});
173+
})
181174
```
182175

183176
### 手工绑定
184177

185-
**手工绑定** 适用于全部场景。做法:使用 `createStoreBindings` 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。
178+
**手工绑定** 更加灵活,适用于 store 需要在 `onLoad` (自定义组件 attached )时才能确定的情况。
179+
180+
做法:使用 `createStoreBindings` 创建绑定,它会返回一个包含清理函数的对象用于取消绑定。
186181

187182
注意:在页面 onUnload (自定义组件 detached )时一定要调用清理函数,否则将导致内存泄漏!
188183

189184
```js
190-
import { createStoreBindings } from "mobx-miniprogram-bindings";
185+
import { createStoreBindings } from 'mobx-miniprogram-bindings'
191186

192187
Page({
193188
onLoad() {
194189
this.storeBindings = createStoreBindings(this, {
195190
/* 绑定配置(见下文) */
196-
});
191+
})
197192
},
198193
onUnload() {
199-
this.storeBindings.destroyStoreBindings();
194+
this.storeBindings.destroyStoreBindings()
200195
},
201-
});
196+
})
202197
```
203198

204199
### 绑定配置
@@ -251,7 +246,7 @@ Page({
251246
Component({
252247
behaviors: [storeBindingsBehavior, computedBehavior],
253248
/* ... */
254-
});
249+
})
255250
```
256251

257252
### 关于部分更新
@@ -263,19 +258,19 @@ Component({
263258
behaviors: [storeBindingsBehavior],
264259
storeBindings: {
265260
store,
266-
fields: ["someObject"],
261+
fields: ['someObject'],
267262
},
268-
});
263+
})
269264
```
270265

271266
如果尝试在 `store` 中:
272267

273268
```js
274-
this.someObject.someField = "xxx";
269+
this.someObject.someField = 'xxx'
275270
```
276271

277272
这样是不会触发界面更新的。请考虑改成:
278273

279274
```js
280-
this.someObject = Object.assign({}, this.someObject, { someField: "xxx" });
275+
this.someObject = Object.assign({}, this.someObject, { someField: 'xxx' })
281276
```

0 commit comments

Comments
 (0)