update mp mvvm - using mapping 2.0

This commit is contained in:
dntzhang 2018-12-09 12:19:07 +08:00
parent 10a6c4bd80
commit 76ce9e5f6f
3 changed files with 87 additions and 67 deletions

View File

@ -11,7 +11,6 @@ create.Page = function (vm, options) {
options.onLoad = function (e) {
vm.data = new JSONProxy(vm.data).observe(false, patch => {
clearTimeout(timeout)
console.log(patch )
if (patch.op === 'remove') {//fix arr splice
const kv = getArrayPatch(patch.path)
patchs[kv.k] = kv.v

View File

@ -1,111 +1,129 @@
/**
* mappingjs v1.0.1 by dntzhang
* mappingjs v2.0.0 by dntzhang
* Objects mapping for javascript. Omi MVVM's best partner.
* @method mapping
* @param {Object} options {from: .., to: .., rule: .. }
* @param {from} options
* @param {to} options
* @param {rule} options
* @return {Object} To Object
*/
var ARRAYTYPE = '[object Array]'
var OBJECTTYPE = '[object Object]'
var mapping = function (options) {
var from = options.from
var to = options.to
var rules = options.rule
function mapping(from, to, rule) {
var tempRule = Object.assign({}, rule)
var res = to || {}
Object.keys(from).forEach(function (key) {
res[key] = from[key]
var obj = from[key]
if (isArray(obj)) {
res[key] = res[key] || []
arrayMapping(obj, res[key], tempRule, key)
} else if (isObject(obj)) {
res[key] = res[key] || {}
objMapping(obj, res[key], tempRule, key)
} else {
res[key] = obj
}
})
rule && Object.keys(tempRule).forEach(function (key) {
var arr = key
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
if (arr.length === 1) {
res[key] = tempRule[key].call ? tempRule[key].call(from) : tempRule[key]
delete tempRule[key]
}
})
rules &&
Object.keys(rules).forEach(function (key) {
var rule = rules[key]
var isPath = key.match(/\.|\[/)
if (typeof rule === 'function') {
if (isPath) {
setPathValue(res, key, rule.call(from))
} else {
res[key] = rule.call(from)
}
} else {
if (isPath) {
setPathValue(res, key, rule)
} else {
res[key] = rule
}
}
})
return res
}
function setPathValue(obj, path, value) {
var arr = path
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
var current = obj
for (var i = 0, len = arr.length; i < len; i++) {
var key = arr[i]
var temp = current[key]
if (i === len - 1) {
current[arr[len - 1]] = value
} else {
if (temp === undefined) {
if (isNaN(Number(arr[i + 1]))) {
current[key] = {}
} else {
current[key] = []
}
temp = current[key]
}
}
current = temp
}
}
mapping.auto = function (from, to) {
return objMapping(from, to)
}
function arrayMapping(from, to) {
function arrayMapping(from, to, rule, path) {
if (from.length < to.length) {
to.length = from.length
}
from.forEach(function (item, index) {
if (isArray(item)) {
to[index] = to[index] || []
arrayMapping(item, to[index])
arrayMapping(item, to[index], rule, path + '[' + index + ']')
} else if (isObject(item)) {
to[index] = objMapping(item, to[index])
to[index] = objMapping(item, to[index], rule, path + '[' + index + ']')
} else {
to[index] = item
}
})
rule && Object.keys(rule).forEach(function (key) {
var arr = key
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
var pathArr = path
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
var dl = arr.length - pathArr.length
if (dl === 1 && equalArr(arr, pathArr)) {
to[arr[arr.length - 1]] = rule[key].call ? rule[key].call(from) : rule[key]
delete rule[key]
}
})
}
function objMapping(from, to) {
function objMapping(from, to, rule, path) {
var res = to || {}
Object.keys(from).forEach(key => {
Object.keys(from).forEach(function (key) {
var obj = from[key]
if (isArray(obj)) {
res[key] = res[key] || []
arrayMapping(obj, res[key])
arrayMapping(obj, res[key], rule, path + '.' + key)
} else if (isObject(obj)) {
res[key] = res[key] || {}
objMapping(obj, res[key])
objMapping(obj, res[key], rule, path + '.' + key)
} else {
res[key] = obj
}
})
rule && Object.keys(rule).forEach(function (key) {
var arr = key
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
var pathArr = path
.replace(/]/g, '')
.replace(/\[/g, '.')
.split('.')
if (arr.length - pathArr.length === 1 && equalArr(arr, pathArr)) {
res[arr[arr.length - 1]] = rule[key].call ? rule[key].call(from) : rule[key]
if (arr.indexOf('*') === -1) {
delete rule[key]
}
}
})
return res
}
function equalArr(arrA, arrB) {
var i = 0, len = arrB.length
for (; i < len; i++) {
if (arrA[i] !== arrB[i] && !(arrA[i] === '*' && !isNaN(Number(arrB[i])))) {
return false
}
}
return true
}
function isArray(obj) {
return Object.prototype.toString.call(obj) === ARRAYTYPE
}
@ -114,6 +132,9 @@ function isObject(obj) {
return Object.prototype.toString.call(obj) === OBJECTTYPE
}
//Compatible with older versions
mapping.auto = mapping
if (typeof exports == "object") {
module.exports = mapping
} else if (typeof define == "function" && define.amd) {

View File

@ -10,7 +10,7 @@ class TodoViewModel {
update() {
//will automatically update the view!!!
mapping.auto(todo, this.data)
mapping(todo, this.data)
}
complete(id) {