mappingjs - sync array length when auto mapping
This commit is contained in:
parent
9f48b5ad91
commit
1d5c30f841
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* mappingjs v1.0.0 by dntzhang
|
||||
* mappingjs v1.0.1 by dntzhang
|
||||
* Objects mapping for javascript. Omi MVVM's best partner.
|
||||
* @method mapping
|
||||
* @param {Object} options {from: .., to: .., rule: .. }
|
||||
|
@ -74,6 +74,9 @@ mapping.auto = function (from, to) {
|
|||
}
|
||||
|
||||
function arrayMapping(from, to) {
|
||||
if (from.length < to.length) {
|
||||
to.length = from.length
|
||||
}
|
||||
from.forEach(function (item, index) {
|
||||
if (isArray(item)) {
|
||||
to[index] = to[index] || []
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mappingjs",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"description": "Objects mapping for javascript. [Omi MVVM](https://github.com/Tencent/omi/blob/master/tutorial/omi-mvvm.md)'s best partner.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* mappingjs v1.0.0 by dntzhang
|
||||
* mappingjs v1.0.1 by dntzhang
|
||||
* Objects mapping for javascript. Omi MVVM's best partner.
|
||||
* @method mapping
|
||||
* @param {Object} options {from: .., to: .., rule: .. }
|
||||
|
@ -9,19 +9,19 @@
|
|||
var ARRAYTYPE = '[object Array]'
|
||||
var OBJECTTYPE = '[object Object]'
|
||||
|
||||
var mapping = function(options) {
|
||||
var mapping = function (options) {
|
||||
var from = options.from
|
||||
var to = options.to
|
||||
var rules = options.rule
|
||||
|
||||
var res = to || {}
|
||||
|
||||
Object.keys(from).forEach(function(key) {
|
||||
Object.keys(from).forEach(function (key) {
|
||||
res[key] = from[key]
|
||||
})
|
||||
|
||||
rules &&
|
||||
Object.keys(rules).forEach(function(key) {
|
||||
Object.keys(rules).forEach(function (key) {
|
||||
var rule = rules[key]
|
||||
var isPath = key.match(/\.|\[/)
|
||||
if (typeof rule === 'function') {
|
||||
|
@ -69,12 +69,15 @@ function setPathValue(obj, path, value) {
|
|||
}
|
||||
}
|
||||
|
||||
mapping.auto = function(from, to) {
|
||||
mapping.auto = function (from, to) {
|
||||
return objMapping(from, to)
|
||||
}
|
||||
|
||||
function arrayMapping(from, to) {
|
||||
from.forEach(function(item, index) {
|
||||
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])
|
||||
|
@ -111,12 +114,10 @@ function isObject(obj) {
|
|||
return Object.prototype.toString.call(obj) === OBJECTTYPE
|
||||
}
|
||||
|
||||
if (typeof exports == 'object') {
|
||||
if (typeof exports == "object") {
|
||||
module.exports = mapping
|
||||
} else if (typeof define == 'function' && define.amd) {
|
||||
define([], function() {
|
||||
return mapping
|
||||
})
|
||||
} else if (typeof define == "function" && define.amd) {
|
||||
define([], function () { return mapping })
|
||||
} else {
|
||||
window.mapping = mapping
|
||||
}
|
||||
|
|
|
@ -1540,7 +1540,7 @@
|
|||
|
||||
var mappingjs = createCommonjsModule(function (module, exports) {
|
||||
/**
|
||||
* mappingjs v1.0.0 by dntzhang
|
||||
* mappingjs v1.0.1 by dntzhang
|
||||
* Objects mapping for javascript. Omi MVVM's best partner.
|
||||
* @method mapping
|
||||
* @param {Object} options {from: .., to: .., rule: .. }
|
||||
|
@ -1615,6 +1615,9 @@
|
|||
};
|
||||
|
||||
function arrayMapping(from, to) {
|
||||
if (from.length < to.length) {
|
||||
to.length = from.length;
|
||||
}
|
||||
from.forEach(function (item, index) {
|
||||
if (isArray(item)) {
|
||||
to[index] = to[index] || [];
|
||||
|
@ -1807,8 +1810,6 @@
|
|||
|
||||
TodoViewModel.prototype.remove = function remove(id) {
|
||||
todo.remove(id);
|
||||
//empty first
|
||||
this.data.items.length = 0;
|
||||
this.update();
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -30,8 +30,6 @@ class TodoViewModel {
|
|||
|
||||
remove(id) {
|
||||
todo.remove(id)
|
||||
//empty first
|
||||
this.data.items.length = 0
|
||||
this.update()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue