Skip to content

Commit

Permalink
feat: Root#assignShallow supports nested Root instance
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Mar 6, 2018
1 parent 9ef97ed commit 4e67065
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:cy:ci": "npm-run-all --race --parallel \"gojs:open -- -s -i binding.js\" \"cy:run -- --record --key {1}\" --",
"test:cy:run": "run-p --race \"gojs -- -s\" cy:run",
"test:cy:open": "run-p gojs cy:open",
"gojs": "gojs examples --noopen -p 8001",
"gojs": "npm run gojs:open -- --noopen",
"gojs:open": "gojs examples -p 8001",
"cy:run": "cypress run",
"cy:open": "cypress open",
Expand Down
28 changes: 18 additions & 10 deletions src/Model/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import _ from 'lodash'

/**
*
*
* 建议所以的 Model 都继承自该类,提供了一些方法
* @typedef Root
* @public
Expand All @@ -22,7 +22,7 @@ export default class Root {
* 建议使用给方法创建实例对象,而不是 `new Model()` 原因参看 https://github.com/imcuttle/babel-plugin-class-properties-default-value
* @static
* @public
* @param {object} [init={}]
* @param {object} [init={}]
* @returns {Root}
* @memberof Root
* @example
Expand Down Expand Up @@ -68,10 +68,10 @@ export default class Root {
}

/**
*
*
* 设置当前对象中 key 的值
* @param {string | array} key
* @param {any} value
* @param {string | array} key
* @param {any} value
* @returns {Root}
* @public
* @memberof Root
Expand All @@ -96,13 +96,21 @@ export default class Root {
data = toJS(data)
for (let key in data) {
if (data.hasOwnProperty(key)) {
this[key] = typeof data[key] === 'undefined' ? this[key] : data[key]
if (typeof data[key] !== 'undefined') {
if (this[key] instanceof Root) {
this[key].assignShallow(data[key])
}
else {
this[key] = data[key]
}
}

}
}
return this
}
/**
*
*
* 拷贝当前对象
* @returns {Root} 一个新的实例
* @public
Expand All @@ -113,7 +121,7 @@ export default class Root {
}
/**
* 对比两个对象是否相同
* @param {any} other
* @param {any} other
* @returns {boolean}
* @memberof Root
* @public
Expand All @@ -129,7 +137,7 @@ export default class Root {

/**
* 批量赋值
* @param {object | Root} data
* @param {object | Root} data
* @returns {Root}
* @memberof Root
* @public
Expand All @@ -142,7 +150,7 @@ export default class Root {
/**
* 深拷贝批量赋值
* @see [assign](#assign)
* @param {object | Root} data
* @param {object | Root} data
* @returns {Root}
* @memberof Root
* @public
Expand Down
19 changes: 19 additions & 0 deletions test/Model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,23 @@ describe('Model.lifeCycle', () => {
expect(exit.callCount).toBe(1)
expect(init.callCount).toBe(2)
})

test('assignShallow nested Root instance', () => {
class Bar extends Root {
val = 'val'
}
class Person extends Bar {
bar = new Bar
}
class Nested extends Root {
person = new Person()
}

const nested = Nested.create({ person: { bar: { val: 'abc' } } })
expect(nested.person instanceof Person).toBe(true)
expect(nested.person instanceof Root).toBe(true)
expect(nested.person.val).toBe('val')
expect(nested.person.bar.val).toBe('abc')
expect(nested.person.bar instanceof Bar).toBe(true)
})
})

0 comments on commit 4e67065

Please sign in to comment.