feat: support computed prop in use

This commit is contained in:
dntzhang 2019-10-04 09:03:30 +08:00
parent f9178f46a2
commit 99e757f1a0
5 changed files with 42 additions and 30 deletions

View File

@ -95,10 +95,7 @@ export default {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
logs: [],
reverseMotto() {
return this.motto.split('').reverse().join('')
}
logs: []
},
debug: true, //我是开关
updateAll: true //当为 true 时,无脑全部更新,组件或页面不需要声明 use
@ -125,15 +122,21 @@ this.oData.arr.purePush(111) //不会触发视图更新
####  函数属性
```js
data: {
motto: 'Hello World',
reverseMotto() {
return this.motto.split('').reverse().join('')
use: [
'motto',
'userInfo',
'hasUserInfo',
'canIUse',
{
reverseMotto:[
['motto'],
motto => motto.split('').reverse().join('')
]
}
},
],
```
其中 reverseMotto 可以直接绑定在 wxml 里motto 更新会自动更新 reverseMotto 的值。
函数属性定义在页面或者组件的 use 里,如上面的 `reverseMotto`可以直接绑定在 wxml 里motto 更新会自动更新 reverseMotto 的值。
## Q & A

View File

@ -10,6 +10,12 @@ create(store, {
'userInfo',
'hasUserInfo',
'canIUse',
{
reverseMotto:[
['motto'],
motto => motto.split('').reverse().join('')
]
}
],
//事件处理函数
bindViewTap: function () {

View File

@ -4,10 +4,7 @@ export default {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
logs: [],
reverseMotto() {
return this.motto.split('').reverse().join('')
}
logs: []
},
//无脑全部更新,组件或页面不需要声明 use
//updateAll: true,

View File

@ -5,7 +5,7 @@
*/
import obaa from './obaa'
import { getPath, needUpdate, fixPath } from './path'
import { getPath, needUpdate, fixPath, getUsing } from './path'
const ARRAYTYPE = '[object Array]'
const OBJECTTYPE = '[object Object]'
@ -28,13 +28,15 @@ function create(store, option) {
this.store = store
option.use && (this.__updatePath = getPath(option.use))
this.__use = option.use
store.instances[this.route] = []
store.instances[this.route].push(this)
if (!option.data.___walked) {
walk(this.store.data)
}
this.setData.call(this, option.data)
this.setData(option.data)
const using = getUsing(store.data, option.use)
using && this.setData(using)
onLoad && onLoad.call(this, e)
}
Page(option)
@ -45,10 +47,11 @@ function create(store, option) {
const page = getCurrentPages()[getCurrentPages().length - 1]
store.use && (this.__updatePath = getPath(store.use))
this.store = page.store
this.__use = store.use
store.data = this.store.data
this.setData.call(this, store.data)
this.setData(store.data)
const using = getUsing(this.store.data, store.use)
using && this.setData(using)
this.store.instances[page.route].push(this)
ready && ready.call(this)
}
@ -82,6 +85,11 @@ function _update(kv, store) {
store.instances[key].forEach(ins => {
if(store.updateAll || ins.__updatePath && needUpdate(kv,ins.__updatePath)){
ins.setData.call(ins, kv)
const using = getUsing(store.data, ins.__use)
using && ins.setData(using)
//即将废弃
updateStoreByFnProp(ins, store.data)
}
})

View File

@ -2,30 +2,28 @@ const OBJECTTYPE = '[object Object]'
const ARRAYTYPE = '[object Array]'
export function getUsing(data, paths) {
const obj = []
if(!paths) return
const obj = {}
paths.forEach((path, index) => {
const isPath = typeof path === 'string'
if (isPath) {
obj[index] = getTargetByPath(data, path)
} else {
if (!isPath) {
const key = Object.keys(path)[0]
const value = path[key]
if (typeof value === 'string') {
obj[index] = getTargetByPath(data, value)
} else {
if (typeof value !== 'string') {
const tempPath = value[0]
if (typeof tempPath === 'string') {
const tempVal = getTargetByPath(data, tempPath)
obj[index] = value[1] ? value[1](tempVal) : tempVal
obj[key] = value[1] ? value[1](tempVal) : tempVal
} else {
const args = []
tempPath.forEach(path => {
args.push(getTargetByPath(data, path))
})
obj[index] = value[1].apply(null, args)
obj[key] = value[1].apply(null, args)
}
}
obj[key] = obj[index]
}
})
return obj