This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0

增加组件props回调的this指向父vm; vm对象增加$children属性,储存所有子组件; vm对象增加$refs属性,储存所有带ref属性的元素; vm对象增加mounted回调

old
宇天 2018-01-25 03:09:55 +08:00
parent 02c5ab1abf
commit 879a022a89
4 changed files with 37 additions and 7 deletions

0
.gitignore vendored Executable file → Normal file
View File

View File

@ -1373,6 +1373,7 @@
var methods = source.methods var methods = source.methods
var props = source.props var props = source.props
var watches = source.watch var watches = source.watch
var mounted = source.mounted
delete source.state delete source.state
delete source.computed delete source.computed
@ -1488,6 +1489,8 @@
hideProperty($vmodel, '$active', false) hideProperty($vmodel, '$active', false)
hideProperty($vmodel, '$pathname', old ? old.$pathname : '') hideProperty($vmodel, '$pathname', old ? old.$pathname : '')
hideProperty($vmodel, '$accessors', accessors) hideProperty($vmodel, '$accessors', accessors)
hideProperty($vmodel, '$refs', {})
hideProperty($vmodel, '$children', [])
hideProperty($vmodel, 'hasOwnProperty', trackBy) hideProperty($vmodel, 'hasOwnProperty', trackBy)
if (options.watch) { if (options.watch) {
hideProperty($vmodel, '$watch', function() { hideProperty($vmodel, '$watch', function() {
@ -1500,6 +1503,12 @@
var v = Anot.vmodels[i] var v = Anot.vmodels[i]
v.$fire && v.$fire.apply(v, [ee, a]) v.$fire && v.$fire.apply(v, [ee, a])
} }
} else if (path.indexOf('child!') === 0) {
var ee = 'props.' + path.slice(6)
for (var i in $vmodel.$children) {
var v = $vmodel.$children[i]
v.$fire && v.$fire.apply(v, [ee, a])
}
} else { } else {
$emit.call($vmodel, path, [a]) $emit.call($vmodel, path, [a])
} }
@ -1527,6 +1536,12 @@
} }
$vmodel.$active = true $vmodel.$active = true
$vmodel.mounted = mounted
if (old && old.$up) {
log(old.$up)
old.$up.$children.push($vmodel)
}
return $vmodel return $vmodel
} }
@ -3226,6 +3241,12 @@
//确保所有:attr-name扫描完再处理 //确保所有:attr-name扫描完再处理
_delay_component(widget) _delay_component(widget)
} }
} else {
// 非组件才检查 ref属性
var ref = isRef(elem)
if (ref) {
vmodels[0].$refs[ref] = elem
}
} }
} }
@ -3311,7 +3332,10 @@
if (newVmodel) { if (newVmodel) {
setTimeout(function() { setTimeout(function() {
newVmodel.$fire(':scan-end', elem) if (typeof newVmodel.mounted === 'function') {
newVmodel.mounted()
}
delete newVmodel.mounted
}) })
} }
} }
@ -3501,7 +3525,7 @@
delete hooks.componentWillUnmount delete hooks.componentWillUnmount
var vmodel = Anot(hooks) var vmodel = Anot(hooks)
vmodel.$refs = {} host.vmodels[0].$children.push(vmodel)
elem.msResolved = 1 //防止二进扫描此元素 elem.msResolved = 1 //防止二进扫描此元素
@ -3550,7 +3574,7 @@
if (ev.childReady) { if (ev.childReady) {
dependencies += ev.childReady dependencies += ev.childReady
if (vmodel !== ev.vm) { if (vmodel !== ev.vm) {
vmodel.$refs[ev.vm.$id] = ev.vm vmodel.$children.push(ev.vm)
ev.vm.$up = vmodel ev.vm.$up = vmodel
if (ev.childReady === -1) { if (ev.childReady === -1) {
children++ children++
@ -3613,6 +3637,10 @@
return null return null
} }
function isRef(el) {
return el.hasAttribute('ref') ? el.getAttribute('ref') : null
}
var bools = [ var bools = [
'autofocus,autoplay,async,allowTransparency,checked,controls', 'autofocus,autoplay,async,allowTransparency,checked,controls',
'declare,disabled,defer,defaultChecked,defaultSelected', 'declare,disabled,defer,defaultChecked,defaultSelected',
@ -3683,6 +3711,7 @@
update: function(val) { update: function(val) {
var elem = this.element var elem = this.element
var obj = val var obj = val
var vm = this.vmodels[0]
if (typeof obj === 'object' && obj !== null) { if (typeof obj === 'object' && obj !== null) {
if (!Anot.isPlainObject(obj)) { if (!Anot.isPlainObject(obj)) {
@ -3749,7 +3778,7 @@
obj[i] = JSON.stringify(obj[i]) obj[i] = JSON.stringify(obj[i])
} else if (typeof obj[i] === 'function') { } else if (typeof obj[i] === 'function') {
k = '__fn__' + camelize(k) k = '__fn__' + camelize(k)
elem[k] = obj[i] elem[k] = obj[i].bind(vm)
obj[i] = k obj[i] = k
} }
elem.setAttribute(k, obj[i]) elem.setAttribute(k, obj[i])

View File

@ -209,8 +209,9 @@ export default Anot.component('datepicker', {
}, },
componentDidMount: function() { componentDidMount: function() {
if (typeof this.props.onCreated === 'function') { if (typeof this.props.onCreated === 'function') {
this.props.onCreated.call(null, this) this.props.onCreated(this)
} }
document.addEventListener('click', () => { document.addEventListener('click', () => {
this.close() this.close()
}) })

View File

@ -39,7 +39,7 @@ function calculate({ currPage, maxPageShow, totalPages }) {
function update(currPage, vm) { function update(currPage, vm) {
const { totalPages, props: { maxPageShow } } = vm const { totalPages, props: { maxPageShow } } = vm
vm.currPage = vm.inputPage = currPage vm.currPage = vm.inputPage = currPage
vm.props.onPageChange.call(null, currPage) vm.props.onPageChange(currPage)
vm.pageList.clear() vm.pageList.clear()
if (totalPages > 1) { if (totalPages > 1) {
vm.pageList.pushArray(calculate({ currPage, totalPages, maxPageShow })) vm.pageList.pushArray(calculate({ currPage, totalPages, maxPageShow }))
@ -97,7 +97,7 @@ export default Anot.component('pager', {
}, },
componentDidMount: function() { componentDidMount: function() {
if (typeof this.props.onCreated === 'function') { if (typeof this.props.onCreated === 'function') {
this.props.onCreated.call(null, this) this.props.onCreated(this)
} }
}, },
state: { state: {