obaa - v2.0.1

* Expose more detailed paths when array updates
This commit is contained in:
dntzhang 2019-07-16 10:15:21 +08:00
parent 31dcaca124
commit 31a20ee163
5 changed files with 51 additions and 14 deletions

View File

@ -27,7 +27,7 @@ var arr = [1, 2, 3];
obaa(arr, function (name, value, old) {
console.log(name + "__" + value+"__"+old);
});
arr.push(4);//Array-push__[1,2,3,4]__[1,2,3]
arr.push(4);//Array-push-#__[1,2,3,4]__[1,2,3]
arr[3] = 5;//3__5__4
```

View File

@ -21,7 +21,7 @@
__p_: '#'
}
}
mock(target, target)
mock(target, target, '#')
}
for (var prop in target) {
if (target.hasOwnProperty(prop)) {
@ -97,7 +97,7 @@
'size'
]
function mock(target, root) {
function mock(target, root, prop) {
methods.forEach(function (item) {
target[item] = function () {
var old = Array.prototype.slice.call(this, 0)
@ -116,7 +116,7 @@
}
//todo
onPropertyChanged(
'Array-' + item,
'Array-' + item + '-' + prop,
this,
old,
this,
@ -171,7 +171,7 @@
})
if (typeof currentValue == 'object') {
if (isArray(currentValue)) {
mock(currentValue, root)
mock(currentValue, root, prop)
if (currentValue.length === 0) {
if (!currentValue.__o_) currentValue.__o_ = {}
if (path !== undefined) {

View File

@ -1,6 +1,6 @@
{
"name": "obaa",
"version": "2.0.0",
"version": "2.0.1",
"description": "Observe any object's any change.",
"main": "index.js",
"scripts": {

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>obaa test</title>
<script src="util.js"></script>
<script src="../index.js"></script>
</head>
<body>
<script>
var arr = []
obaa(arr,function(a,b,c,d){
console.log(a,b,c,d)
})
arr.push('11')
var obj = {arr:[],d: 1,c:{d:3,e:[]}}
obaa(obj,function(a,b,c,d){
console.log(a,b,c,d)
})
obj.arr.push('11')
obj.arr.push('22')
obj.c.e.push(4)
</script>
</body>
</html>

View File

@ -11,15 +11,15 @@
<script>
var result = [
['a', { b: 1 }, 1, '#'],
['b', 'abc', 1, '#-a'],
['c', 3, undefined, '#'],
['c', 4, 3, '#'],
['d', 5, undefined, '#'],
['h', {i:100}, undefined, '#-e'],
['h', { i: 100 }, undefined, '#-e'],
['i', 22, 100, '#-e-h'],
["1" ,3, 2, "#"]
["1", 3, 2, "#"]
]
</script>
@ -47,9 +47,9 @@
obaa.add(obj, 'd');
obj.d = 5;//c__4__3__#
obaa.set(obj.e, 'h', {i:100})
obaa.set(obj.e, 'h', { i: 100 })
obj.e.h.i = 22
log(count === 7)
</script>
@ -58,20 +58,20 @@
obaa(arr, function (prop, value, old, path) {
count++
if (count === 9) {
log(deepEqual(prop, 'Array-push'), deepEqual(value[2], 'abc'), deepEqual(old, [1, 3]), deepEqual(path, '#'))
log(deepEqual(prop, 'Array-push-#'), deepEqual(value[2], 'abc'), deepEqual(old, [1, 3]), deepEqual(path, '#'))
} else if (count === 10) {
} else if (count === 11) {
log(prop === 'newItem', value === 22, old === 11, path === '#-3')
} else if (count === 12) {
log(prop === 'Array-size', value.length === 2, old.length === 4, path === '#')
log(prop === 'Array-size-#', value.length === 2, old.length === 4, path === '#')
} else if (count === 13) {
//console.log(prop, value, old, path)
} else if(count==8){
} else if (count == 8) {
var item = result.shift()
log(deepEqual(prop, item[0]), deepEqual(value, item[1]), deepEqual(old, item[2]), deepEqual(path, item[3]))
}