From 89363ead86ac5ac44fdaf0ba678beac6f3510df4 Mon Sep 17 00:00:00 2001 From: Steve Young <2747745470@qq.com> Date: Fri, 31 Aug 2018 17:54:56 +0800 Subject: [PATCH] Feature/set data (#55) * chore: add npmignore and ignore pattern * feat(tua-mp): hack native setData * chore: move some devDependencies into packages * refactor(tua-mp): index -> TuaPage/TuaComp, add utils/hackSetData * docs: update basic example, add setData example * perf: add error message when call setData with uninitialized properties * docs: close #41, update mini code url, add hack setData part * perf: modify vm.data when call setData --- README.md | 4 +- docs/README.md | 2 +- docs/quick-start/README.md | 30 ++- lerna.json | 4 +- package.json | 6 - packages/tua-mp-cli/.npmignore | 5 + packages/tua-mp-cli/package.json | 8 +- packages/tua-mp-service/.npmignore | 4 + packages/tua-mp-service/package.json | 5 + packages/tua-mp/__tests__/TuaComp.test.js | 203 ++++++++++++++++++ .../{index.test.js => TuaPage.test.js} | 196 +---------------- packages/tua-mp/__tests__/setData.test.js | 52 +++++ packages/tua-mp/__tests__/utils/basic.test.js | 2 +- .../basic/comps/view-button/view-button.js | 10 +- .../tua-mp/examples/basic/pages/test/test.js | 32 ++- .../examples/basic/pages/test/test.wxml | 20 +- .../tua-mp/examples/basic/project.config.json | 10 +- .../tua-mp/examples/basic/utils/tua-mp.js | 129 +++++++---- packages/tua-mp/package.json | 22 +- packages/tua-mp/src/TuaComp.js | 89 ++++++++ packages/tua-mp/src/TuaPage.js | 77 +++++++ packages/tua-mp/src/VmStatus.js | 8 +- packages/tua-mp/src/index.js | 145 +------------ packages/tua-mp/src/utils/basic.js | 36 +++- packages/tua-mp/src/utils/hackSetData.js | 26 +++ packages/tua-mp/src/utils/index.js | 1 + 26 files changed, 688 insertions(+), 438 deletions(-) create mode 100644 packages/tua-mp-cli/.npmignore create mode 100644 packages/tua-mp-service/.npmignore create mode 100644 packages/tua-mp/__tests__/TuaComp.test.js rename packages/tua-mp/__tests__/{index.test.js => TuaPage.test.js} (66%) create mode 100644 packages/tua-mp/__tests__/setData.test.js create mode 100644 packages/tua-mp/src/TuaComp.js create mode 100644 packages/tua-mp/src/TuaPage.js create mode 100644 packages/tua-mp/src/utils/hackSetData.js diff --git a/README.md b/README.md index 030d2b7..32b1eca 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@
- + @@ -28,7 +28,7 @@ ## 1.1.最基础的使用方式 -- [examples/basic/](https://github.com/tuateam/tua-mp/tree/master/packages/tua-mp/examples/basic) 下载 [https://github.com/tuateam/tua-mp/blob/master/packages/tua-mp/examples/basic/utils/tua-mp.js](https://github.com/tuateam/tua-mp/blob/master/packages/tua-mp/examples/basic/utils/tua-mp.js) 文件到你的小程序项目中,例如保存为 `utils/tua-mp.js`。 -代码片段地址为:**wechatide://minicode/JzXSn8mb78n8** +代码片段地址为:**wechatide://minicode/2n17t5mU752Z** > 可以尝试复制以上片段地址到浏览器地址栏中打开 diff --git a/docs/README.md b/docs/README.md index 45d46be..e4a87ca 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,7 +27,7 @@ footer: MIT Licensed | Copyright © 2018-present StEve Young
-
+
diff --git a/docs/quick-start/README.md b/docs/quick-start/README.md
index 2171e54..6c77004 100644
--- a/docs/quick-start/README.md
+++ b/docs/quick-start/README.md
@@ -7,7 +7,7 @@
官方指南假设你已了解关于 [微信小程序开发](https://developers.weixin.qq.com/miniprogram/dev/index.html) 和 [Vue.js](https://cn.vuejs.org/v2/guide/index.html) 的基础知识。
:::
-尝试 `tua-mp` 最简单的方法是 [👉点击这里打开代码片段👈](wechatide://minicode/JzXSn8mb78n8),这个操作会打开你的**微信开发者工具**,并导入代码片段。(详情可参阅 [代码片段文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html))
+尝试 `tua-mp` 最简单的方法是 [👉点击这里打开代码片段👈](wechatide://minicode/2n17t5mU752Z),这个操作会打开你的**微信开发者工具**,并导入代码片段。(详情可参阅 [代码片段文档](https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html))
如果还没有安装 【微信开发者工具】 [👉点击这里下载👈](https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html)
@@ -38,9 +38,35 @@ TuaPage({
我们已经成功创建了第一个 `tua-mp` 应用!看起来这跟渲染一个字符串模板非常类似,但是 `tua-mp` 在背后做了一点微小的工作。
-现在改变数据已经不需要调用 `setData`,所有数据都是响应式的。我们要怎么确认呢?[👉点击这里打开代码片段👈](wechatide://minicode/JzXSn8mb78n8),在控制台里修改 `global.msg` 的值,你将看到上例相应地更新。
+现在改变数据已经不需要调用 `setData`,所有数据都是响应式的。我们要怎么确认呢?[👉点击这里打开代码片段👈](wechatide://minicode/2n17t5mU752Z),在控制台里修改 `global.msg` 的值,你将看到上例相应地更新。
```js
// 在开发者工具的控制台中
global.msg = 'young'
```
+
+## hack 原生 setData
+在 `v0.8.0` 中对于原生的 `setData` 方法进行了 hack。例如:
+
+```js {6}
+TuaPage({
+ data: { msg: 'steve' },
+ computed: { msgPlus: vm => vm.msg + '+' },
+ created () {
+ // 使用 setData 也会触发 computed
+ this.setData({ msg: 'young' })
+ },
+})
+```
+
+::: tip
+之所以选择 hack `setData` 方法,是因为在改造旧项目时,可能已经有了大量的 `setData` 代码。这样在将其向 [过渡版本](./simple-app.md) 改造的过程中,使用 `setData` 更新的数据不是响应式(Reactive)的,因此重构过程可能十分痛苦...
+
+因此 hack 了 `setData` 之后,只需替换 Page、Component 为 TuaPage、TuaComp 后即可马上使用 computed、watch 等特性,页面依然能够跑起来。这样就可以渐进式地将小程序风格的旧代码改为 Vue 风格代码。
+:::
+
+::: warning
+虽然小程序原生支持对于未在 data 中定义的数据进行 `setData`,但是在 `tua-mp` 中如果该属性未定义则会报错!
+
+建议在 `data` 选项中先定义该数据![点我查看更多](https://cn.vuejs.org/v2/guide/reactivity.html#%E5%A3%B0%E6%98%8E%E5%93%8D%E5%BA%94%E5%BC%8F%E5%B1%9E%E6%80%A7)
+:::
diff --git a/lerna.json b/lerna.json
index 4bc0b7a..8589102 100644
--- a/lerna.json
+++ b/lerna.json
@@ -2,7 +2,9 @@
"command": {
"publish": {
"ignoreChanges": [
- "*.md"
+ "**/__mocks__/**",
+ "**/__tests__/**",
+ "**/*.md"
]
}
},
diff --git a/package.json b/package.json
index 2e1aefe..0870a3d 100644
--- a/package.json
+++ b/package.json
@@ -27,9 +27,6 @@
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
- "codecov": "^3.0.2",
- "cross-env": "^5.2.0",
- "eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-node": "^6.0.1",
@@ -37,12 +34,9 @@
"eslint-plugin-standard": "^3.1.0",
"gh-pages": "^1.2.0",
"husky": "^1.0.0-rc.13",
- "jest": "^23.3.0",
"lerna": "^3.0.3",
"lint-staged": "^7.2.2",
"metro-memory-fs": "^0.43.5",
- "rimraf": "^2.6.2",
- "rollup": "^0.59.4",
"rollup-plugin-babel": "^3.0.4",
"rollup-plugin-eslint": "^4.0.0",
"rollup-plugin-json": "^3.0.0",
diff --git a/packages/tua-mp-cli/.npmignore b/packages/tua-mp-cli/.npmignore
new file mode 100644
index 0000000..42c0857
--- /dev/null
+++ b/packages/tua-mp-cli/.npmignore
@@ -0,0 +1,5 @@
+__mocks__/
+__tests__/
+
+src/
+coverage/
diff --git a/packages/tua-mp-cli/package.json b/packages/tua-mp-cli/package.json
index 7e3a9e8..be30644 100644
--- a/packages/tua-mp-cli/package.json
+++ b/packages/tua-mp-cli/package.json
@@ -12,7 +12,7 @@
"lint": "eslint --fix lib/**/*.js bin/*",
"clean": "rimraf src && mkdir src/ src/app/ src/apis/ src/pages/ src/comps/ && echo {} > src/app/app.json",
"precommit": "lint-staged",
- "pub": "yarn test && npm publish"
+ "pub": "npm test && npm publish"
},
"lint-staged": {
"bin/*": [
@@ -69,5 +69,11 @@
},
"engines": {
"node": ">=8"
+ },
+ "devDependencies": {
+ "codecov": "^3.0.4",
+ "cross-env": "^5.2.0",
+ "jest": "^23.5.0",
+ "rimraf": "^2.6.2"
}
}
diff --git a/packages/tua-mp-service/.npmignore b/packages/tua-mp-service/.npmignore
new file mode 100644
index 0000000..8d86ff1
--- /dev/null
+++ b/packages/tua-mp-service/.npmignore
@@ -0,0 +1,4 @@
+__mocks__/
+__tests__/
+
+coverage/
diff --git a/packages/tua-mp-service/package.json b/packages/tua-mp-service/package.json
index 6647f92..f9ab148 100644
--- a/packages/tua-mp-service/package.json
+++ b/packages/tua-mp-service/package.json
@@ -81,5 +81,10 @@
},
"engines": {
"node": ">=8"
+ },
+ "devDependencies": {
+ "codecov": "^3.0.4",
+ "cross-env": "^5.2.0",
+ "jest": "^23.5.0"
}
}
diff --git a/packages/tua-mp/__tests__/TuaComp.test.js b/packages/tua-mp/__tests__/TuaComp.test.js
new file mode 100644
index 0000000..573b601
--- /dev/null
+++ b/packages/tua-mp/__tests__/TuaComp.test.js
@@ -0,0 +1,203 @@
+import { afterSetData } from './utils'
+import { TuaComp } from '../src'
+
+const event = {
+ "currentTarget": {
+ "id": "",
+ "offsetLeft": 0,
+ "offsetTop": 0,
+ "dataset": { "index": 0 }
+ },
+ "detail": { "value": [] }
+}
+
+const eventVal = { "value": [], "index": 0 }
+
+const getTestForReservedKeys = (type) => {
+ expect(() => type({ data: { $data: 'young' } })).toThrow()
+ expect(() => type({ data: { __TUA_PATH__: 'path' } })).toThrow()
+ expect(() => type({ computed: { $computed () { return '!' } } })).toThrow()
+ expect(() => type({ methods: { $data () {} } })).toThrow()
+}
+
+describe('TuaComp', () => {
+ test('component lifecycle', () => {
+ let lifecycleArr = []
+
+ const vm = TuaComp({
+ data: { lc: 'a' },
+ beforeCreate () {
+ lifecycleArr.push('beforeCreate')
+ },
+ created () {
+ lifecycleArr.push('created')
+ },
+ beforeMount () {
+ lifecycleArr.push('beforeMount')
+ },
+ ready () {
+ lifecycleArr.push('ready')
+ },
+ mounted () {
+ lifecycleArr.push('mounted')
+ },
+ beforeUpdate () {
+ lifecycleArr.push('beforeUpdate')
+ },
+ updated () {
+ lifecycleArr.push('updated')
+ },
+ beforeDestroy () {
+ lifecycleArr.push('beforeDestroy')
+ },
+ destroyed () {
+ lifecycleArr.push('destroyed')
+ },
+ })
+
+ expect(lifecycleArr).toEqual(['beforeCreate', 'created', 'beforeMount', 'ready', 'mounted'])
+
+ lifecycleArr = []
+ vm.lc = 'b'
+
+ afterSetData(() => {
+ expect(lifecycleArr).toEqual(['beforeUpdate', 'updated'])
+
+ lifecycleArr = []
+ vm.detached()
+ expect(lifecycleArr).toEqual(['beforeDestroy', 'destroyed'])
+ })
+ })
+
+ test('throw error when using reserved keys', () => getTestForReservedKeys(TuaComp))
+
+ test('deep and immediate watch', (done) => {
+ const vm = TuaComp({
+ data () {
+ return {
+ steve: 'young',
+ a: { b: { c: 'd' } },
+ }
+ },
+ computed: {
+ e () { return this.steve + this.a.b.c },
+ },
+ watch: {
+ 'a.b': {
+ deep: true,
+ immediate: true,
+ handler (newVal, oldVal) {
+ this.newAB = newVal
+ this.oldAB = oldVal
+ },
+ },
+ e: { immediate: true, handler: 'eFn' },
+ },
+ methods: {
+ eFn (val) { this.newE = val },
+ },
+ })
+
+ expect(vm.newE).toBe(vm.e)
+ expect(vm.e).toBe(vm.steve + 'd')
+ expect(vm.newAB).toEqual({ c: 'd' })
+ expect(vm.oldAB).toEqual(undefined)
+ vm.a.b.c = 'e'
+
+ afterSetData(() => {
+ expect(vm.e).toBe(vm.steve + 'e')
+ expect(vm.newAB).toEqual({ c: 'e' })
+ expect(vm.oldAB).toEqual({ c: 'e' })
+ done()
+ })
+ })
+
+ test('use it just like MINA', (done) => {
+ const vm = TuaComp({
+ properties: {
+ propA: String,
+ propB: {
+ type: Number,
+ },
+ propC: {
+ type: String,
+ value: 'steve',
+ },
+ },
+ data: { compData: 'compData' },
+ detached () {},
+ methods: {
+ onChangeVal (e) {
+ this.$emit('onChangeVal', e)
+ },
+ onEmitVal () {
+ this.$emit('onEmitVal')
+ },
+ triggerEvent: jest.fn(),
+ },
+ })
+
+ vm.compData = 'steve'
+ expect(vm.propA).toBe('')
+ expect(vm.propB).toBe(0)
+ expect(vm.propC).toBe('steve')
+ expect(vm.compData).toBe('steve')
+
+ vm.onChangeVal(event)
+ expect(vm.triggerEvent).toBeCalledWith('onChangeVal', eventVal, undefined)
+
+ vm.onEmitVal()
+ expect(vm.triggerEvent).toBeCalledWith('onEmitVal', {}, undefined)
+
+ afterSetData(() => {
+ vm.detached()
+ expect(vm.data.compData).toBe('steve')
+ done()
+ })
+ })
+
+ test('use it just like Vue', () => {
+ const vm = TuaComp({
+ props: {
+ propA: Number,
+ propB: [String, Number],
+ propC: { type: String, required: true },
+ propD: { type: Number, default: 100 },
+ propE: { type: Object, default: () => ({ message: 'hello' }) },
+ propF: { validator: val => ['success', 'warning', 'danger'].indexOf(val) !== -1 },
+ },
+ computed: {
+ dAndE () { return this.propD + this.propE.message },
+ },
+ })
+ expect(vm.propA).toBe(0)
+ expect(vm.propB).toBe('')
+ expect(vm.propC).toBe('')
+ expect(vm.propD).toBe(100)
+ expect(vm.propE.message).toBe('hello')
+ expect(vm.propF).toBe('')
+ expect(vm.dAndE).toBe('100hello')
+ })
+
+ test('edge case', () => {
+ const data = jest.fn(() => ({}))
+ const attached = jest.fn()
+ TuaComp({ data, attached })
+
+ expect(data).toBeCalled()
+ expect(attached).toBeCalled()
+ })
+
+ // close #37
+ test('not plain object data', () => {
+ const now = new Date()
+ const hour = now.getHours()
+ const vm = TuaComp({
+ data () { return { now } },
+ computed: {
+ hour () { return this.now.getHours() },
+ },
+ })
+ expect(hour).toBe(vm.hour)
+ })
+})
diff --git a/packages/tua-mp/__tests__/index.test.js b/packages/tua-mp/__tests__/TuaPage.test.js
similarity index 66%
rename from packages/tua-mp/__tests__/index.test.js
rename to packages/tua-mp/__tests__/TuaPage.test.js
index 784c65f..a3aaea6 100644
--- a/packages/tua-mp/__tests__/index.test.js
+++ b/packages/tua-mp/__tests__/TuaPage.test.js
@@ -1,17 +1,5 @@
+import { TuaPage } from '../src'
import { afterSetData } from './utils'
-import { TuaComp, TuaPage } from '../src'
-
-const event = {
- "currentTarget": {
- "id": "",
- "offsetLeft": 0,
- "offsetTop": 0,
- "dataset": { "index": 0 }
- },
- "detail": { "value": [] }
-}
-
-const eventVal = { "value": [], "index": 0 }
const getTestForReservedKeys = (type) => {
expect(() => type({ data: { $data: 'young' } })).toThrow()
@@ -20,188 +8,6 @@ const getTestForReservedKeys = (type) => {
expect(() => type({ methods: { $data () {} } })).toThrow()
}
-describe('TuaComp', () => {
- test('component lifecycle', () => {
- let lifecycleArr = []
-
- const vm = TuaComp({
- data: { lc: 'a' },
- beforeCreate () {
- lifecycleArr.push('beforeCreate')
- },
- created () {
- lifecycleArr.push('created')
- },
- beforeMount () {
- lifecycleArr.push('beforeMount')
- },
- ready () {
- lifecycleArr.push('ready')
- },
- mounted () {
- lifecycleArr.push('mounted')
- },
- beforeUpdate () {
- lifecycleArr.push('beforeUpdate')
- },
- updated () {
- lifecycleArr.push('updated')
- },
- beforeDestroy () {
- lifecycleArr.push('beforeDestroy')
- },
- destroyed () {
- lifecycleArr.push('destroyed')
- },
- })
-
- expect(lifecycleArr).toEqual(['beforeCreate', 'created', 'beforeMount', 'ready', 'mounted'])
-
- lifecycleArr = []
- vm.lc = 'b'
-
- afterSetData(() => {
- expect(lifecycleArr).toEqual(['beforeUpdate', 'updated'])
-
- lifecycleArr = []
- vm.detached()
- expect(lifecycleArr).toEqual(['beforeDestroy', 'destroyed'])
- })
- })
-
- test('throw error when using reserved keys', () => getTestForReservedKeys(TuaComp))
-
- test('deep and immediate watch', (done) => {
- const vm = TuaComp({
- data () {
- return {
- steve: 'young',
- a: { b: { c: 'd' } },
- }
- },
- computed: {
- e () { return this.steve + this.a.b.c },
- },
- watch: {
- 'a.b': {
- deep: true,
- immediate: true,
- handler (newVal, oldVal) {
- this.newAB = newVal
- this.oldAB = oldVal
- },
- },
- e: { immediate: true, handler: 'eFn' },
- },
- methods: {
- eFn (val) { this.newE = val },
- },
- })
-
- expect(vm.newE).toBe(vm.e)
- expect(vm.e).toBe(vm.steve + 'd')
- expect(vm.newAB).toEqual({ c: 'd' })
- expect(vm.oldAB).toEqual(undefined)
- vm.a.b.c = 'e'
-
- afterSetData(() => {
- expect(vm.e).toBe(vm.steve + 'e')
- expect(vm.newAB).toEqual({ c: 'e' })
- expect(vm.oldAB).toEqual({ c: 'e' })
- done()
- })
- })
-
- test('use it just like MINA', (done) => {
- const vm = TuaComp({
- properties: {
- propA: String,
- propB: {
- type: Number,
- },
- propC: {
- type: String,
- value: 'steve',
- },
- },
- data: { compData: 'compData' },
- detached () {},
- methods: {
- onChangeVal (e) {
- this.$emit('onChangeVal', e)
- },
- onEmitVal () {
- this.$emit('onEmitVal')
- },
- triggerEvent: jest.fn(),
- },
- })
-
- vm.compData = 'steve'
- expect(vm.propA).toBe('')
- expect(vm.propB).toBe(0)
- expect(vm.propC).toBe('steve')
- expect(vm.compData).toBe('steve')
-
- vm.onChangeVal(event)
- expect(vm.triggerEvent).toBeCalledWith('onChangeVal', eventVal, undefined)
-
- vm.onEmitVal()
- expect(vm.triggerEvent).toBeCalledWith('onEmitVal', {}, undefined)
-
- afterSetData(() => {
- vm.detached()
- expect(vm.data.compData).toBe('steve')
- done()
- })
- })
-
- test('use it just like Vue', () => {
- const vm = TuaComp({
- props: {
- propA: Number,
- propB: [String, Number],
- propC: { type: String, required: true },
- propD: { type: Number, default: 100 },
- propE: { type: Object, default: () => ({ message: 'hello' }) },
- propF: { validator: val => ['success', 'warning', 'danger'].indexOf(val) !== -1 },
- },
- computed: {
- dAndE () { return this.propD + this.propE.message },
- },
- })
- expect(vm.propA).toBe(0)
- expect(vm.propB).toBe('')
- expect(vm.propC).toBe('')
- expect(vm.propD).toBe(100)
- expect(vm.propE.message).toBe('hello')
- expect(vm.propF).toBe('')
- expect(vm.dAndE).toBe('100hello')
- })
-
- test('edge case', () => {
- const data = jest.fn(() => ({}))
- const attached = jest.fn()
- TuaComp({ data, attached })
-
- expect(data).toBeCalled()
- expect(attached).toBeCalled()
- })
-
- // close #37
- test('not plain object data', () => {
- const now = new Date()
- const hour = now.getHours()
- const vm = TuaComp({
- data () { return { now } },
- computed: {
- hour () { return this.now.getHours() },
- },
- })
- expect(hour).toBe(vm.hour)
- })
-})
-
describe('TuaPage', () => {
test('page lifecycle', () => {
let lifecycleArr = []
diff --git a/packages/tua-mp/__tests__/setData.test.js b/packages/tua-mp/__tests__/setData.test.js
new file mode 100644
index 0000000..4adcb9b
--- /dev/null
+++ b/packages/tua-mp/__tests__/setData.test.js
@@ -0,0 +1,52 @@
+import { afterSetData } from './utils'
+import { TuaComp, TuaPage } from '../src'
+
+describe('TuaComp', () => {
+ afterEach(() => {})
+
+ test('nested setData with callback', (done) => {
+ const vm = TuaComp({
+ data: {
+ a: { b: 'c' },
+ young: 'young',
+ },
+ computed: {
+ sAndY: vm => vm.a.b + vm.young,
+ },
+ })
+
+ vm.setData({ 'a.b': 'steve' }, () => {
+ expect(vm.a.b).toBe('steve')
+ })
+
+ afterSetData(() => {
+ expect(vm.sAndY).toBe('steveyoung')
+ done()
+ })
+ })
+})
+
+describe('TuaPage', () => {
+ afterEach(() => {})
+
+ test('nested setData with callback', (done) => {
+ const vm = TuaPage({
+ data: {
+ a: { b: 'c' },
+ young: 'young',
+ },
+ computed: {
+ sAndY: vm => vm.a.b + vm.young,
+ },
+ })
+
+ vm.setData({ 'a.b': 'steve' }, () => {
+ expect(vm.a.b).toBe('steve')
+ })
+
+ afterSetData(() => {
+ expect(vm.sAndY).toBe('steveyoung')
+ done()
+ })
+ })
+})
diff --git a/packages/tua-mp/__tests__/utils/basic.test.js b/packages/tua-mp/__tests__/utils/basic.test.js
index ea21fb4..266d0d9 100644
--- a/packages/tua-mp/__tests__/utils/basic.test.js
+++ b/packages/tua-mp/__tests__/utils/basic.test.js
@@ -85,7 +85,7 @@ test('setObjByPath', () => {
const obj = {}
setObjByPath({ obj, path: 'a.b.c', val: 1 })
setObjByPath({ obj, path: 'a.b.d', val: 2 })
- setObjByPath({ obj, path: 'arr[0].b.c', val: 3 })
+ setObjByPath({ obj, path: 'arr[0].b.c', val: 3, isCheckDef: true })
expect(obj.a.b.c).toEqual(1)
expect(obj.a.b.d).toEqual(2)
diff --git a/packages/tua-mp/examples/basic/comps/view-button/view-button.js b/packages/tua-mp/examples/basic/comps/view-button/view-button.js
index 0e38b29..85c71cc 100644
--- a/packages/tua-mp/examples/basic/comps/view-button/view-button.js
+++ b/packages/tua-mp/examples/basic/comps/view-button/view-button.js
@@ -11,7 +11,7 @@ TuaComp({
type: String,
// 并没有什么用,因为小程序组件没这概念
required: true,
- // 强行让含 msg 的内容告警
+ // 强行让不含 msg 的内容告警
validator: val => val.indexOf('msg') !== -1,
}
},
@@ -26,19 +26,19 @@ TuaComp({
},
created () {
- console.log('created')
+ console.log('[view-button]: created')
},
mounted () {
- console.log('mounted')
+ console.log('[view-button]: mounted')
},
beforeUpdate () {
- console.log('beforeUpdate')
+ console.log('[view-button]: beforeUpdate')
},
updated () {
- console.log('updated')
+ console.log('[view-button]: updated')
},
/**
diff --git a/packages/tua-mp/examples/basic/pages/test/test.js b/packages/tua-mp/examples/basic/pages/test/test.js
index a2b3857..29b4cbc 100644
--- a/packages/tua-mp/examples/basic/pages/test/test.js
+++ b/packages/tua-mp/examples/basic/pages/test/test.js
@@ -9,7 +9,7 @@ import {
let n = 0
-const watchLog = (prefix) => (newVal, oldVal) => console.log(`${prefix}: ${oldVal} -> ${newVal}`)
+const watchLog = (prefix) => (newVal, oldVal) => console.log(`[WATCH]: ${prefix}: ${oldVal} -> ${newVal}`)
TuaPage({
data () {
@@ -72,13 +72,13 @@ TuaPage({
// 在 setData 前调用
beforeUpdate () {
console.log('beforeUpdate')
- this.stringifyLog(this.data)
+ // this.stringifyLog(this.data)
},
// 在 setData 第二个参数中调用(渲染完毕的回调)
updated () {
console.log('updated')
- this.stringifyLog(this.data)
+ // this.stringifyLog(this.data)
},
computed: {
@@ -92,21 +92,17 @@ TuaPage({
return this.g + ' + ' + this.reversedG
},
countPlus: {
- get: function () {
- return this.count + 1
- },
- set: function (v) {
+ get: vm => vm.count + 1,
+ set (v) {
this.count = v - 1
},
},
},
watch: {
- msg (newVal, oldVal) {
- console.log(`msg: ${oldVal} -> ${newVal}`)
- },
+ msg: watchLog('msg'),
'a.b' (newVal, oldVal) {
- console.log(`a.b: ${oldVal} -> ${newVal}`)
+ console.log(`[WATCH]: a.b: ${oldVal} -> ${newVal}`)
setTimeout(() => {
this.msg = this.reverseStr(this.msg)
}, 1000)
@@ -121,9 +117,7 @@ TuaPage({
// 直接填写 methods 名称
handler: 'logFromMethods',
},
- 'gAndAB' (newVal, oldVal) {
- console.log(`gAndAB: ${oldVal} -> ${newVal}`)
- },
+ 'gAndAB': watchLog('gAndAB'),
// deep
arr: [
watchLog('arr'),
@@ -148,6 +142,9 @@ TuaPage({
tapAB () {
this.a.b += n++
},
+ tapSetDataAB () {
+ this.setData({ 'a.b': this.a.b + n++ })
+ },
tapArr () {
this.arr.push(n++)
},
@@ -169,14 +166,11 @@ TuaPage({
unshiftNested () {
this.arr.unshift({ c: { d: 'hey' } })
},
- gotoLogs () {
- wx.navigateTo({ url: '/pages/logs/logs' })
- },
tapSetCountPlus () {
- this.countPlus = 101
+ this.countPlus += 1
},
tapSetReversedG () {
- this.reversedG = 229
+ this.reversedG = 'whatever value'
},
},
})
diff --git a/packages/tua-mp/examples/basic/pages/test/test.wxml b/packages/tua-mp/examples/basic/pages/test/test.wxml
index 3fa7d21..599cb77 100644
--- a/packages/tua-mp/examples/basic/pages/test/test.wxml
+++ b/packages/tua-mp/examples/basic/pages/test/test.wxml
@@ -1,5 +1,5 @@
-