Skip to content

Commit

Permalink
fix(ob): fix the error when non-object item inherits dep from array
Browse files Browse the repository at this point in the history
  • Loading branch information
BuptStEve committed Jul 12, 2018
1 parent ba65f88 commit dd0ce1c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,21 @@ describe('TuaPage', () => {
const vm = TuaPage({
data () {
return {
strArr: ['a', 'b'],
nestedArr: [ { num: n++ } ],
}
},
computed: {
fromNestedArr () {
return this.nestedArr.filter(x => x.num > 1)
},
firstStr () {
return this.strArr[0]
},
},
})

vm.strArr = ['c']
vm.nestedArr = [ { num: n++ }, { num: n++ } ]
vm.nestedArr.push({ num: n++ })

Expand All @@ -547,6 +552,7 @@ describe('TuaPage', () => {
expect(vm.data.fromNestedArr.length).toBe(0)

afterSetData(() => {
expect(vm.firstStr).toBe('c')
expect(vm.fromNestedArr.length).toBe(3)
expect(vm.nestedArr.__dep__.subs.length)
.toEqual(vm.nestedArr[0].__dep__.subs.length)
Expand Down
6 changes: 4 additions & 2 deletions examples/basic/utils/tua-mp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var version = "0.7.0";
var version = "0.7.1";

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
Expand Down Expand Up @@ -828,8 +828,10 @@ var getObserveDeep = function getObserveDeep(asyncSetData) {

if (Array.isArray(obj)) {
var arr = obj.map(function (item, idx) {
var isNeedInheritDep = (typeof item === 'undefined' ? 'undefined' : _typeof(item)) === 'object' && !item[__dep__] && obj[__dep__];

// 继承依赖
if (!item[__dep__] && obj[__dep__]) {
if (isNeedInheritDep) {
item[__dep__] = obj[__dep__];
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-mp",
"version": "0.7.0",
"version": "0.7.1",
"description": "A helper for coding miniprogram like Vue",
"main": "examples/basic/utils/tua-mp.js",
"files": [
Expand Down
7 changes: 6 additions & 1 deletion src/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ export const getObserveDeep = (asyncSetData) => {
return function observeDeep (obj, prefix = '') {
if (Array.isArray(obj)) {
const arr = obj.map((item, idx) => {
const isNeedInheritDep =
typeof item === 'object' &&
!item[__dep__] &&
obj[__dep__]

// 继承依赖
if (!item[__dep__] && obj[__dep__]) {
if (isNeedInheritDep) {
item[__dep__] = obj[__dep__]
}

Expand Down

0 comments on commit dd0ce1c

Please sign in to comment.