Skip to content

Commit

Permalink
Merge pull request #1409 from didi/feat-relation-api
Browse files Browse the repository at this point in the history
Feat getRelationNodes api
  • Loading branch information
hiyuki authored Feb 27, 2024
2 parents 5346b52 + 03cc6ef commit aa0478d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions packages/core/src/platform/builtInMixins/relationsMixin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isObject } from '@mpxjs/utils'
import { CREATED, MOUNTED, BEFOREUNMOUNT } from '../../core/innerLifecycle'
import { BEFORECREATE, MOUNTED, BEFOREUNMOUNT } from '../../core/innerLifecycle'

const targets = []
let curTarget = null
Expand Down Expand Up @@ -177,17 +177,24 @@ export default function relationsMixin (mixinType) {
}
} else if (__mpx_mode__ === 'web' && mixinType === 'component') {
return {
[CREATED] () {
[BEFORECREATE] () {
this.__mpxRelations = {}
this.__mpxRelationNodesMap = {} // 用于getRelationNodes关系查询
},
[MOUNTED] () {
this.__mpxCollectRelations()
this.__mpxExecRelations('linked')
},
[BEFOREUNMOUNT] () {
this.__mpxExecRelations('unlinked')
// 重置缓存数据
this.__mpxRelations = {}
this.__mpxRelationNodesMap = {}
},
methods: {
getRelationNodes (path) {
return this.__mpxRelationNodesMap[path] || null
},
__mpxCollectRelations () {
const relations = this.__mpxProxy.options.relations
if (!relations) return
Expand Down Expand Up @@ -221,6 +228,7 @@ export default function relationsMixin (mixinType) {
targetRelation,
relation
}
this.__mpxRelationNodesMap[path] = [target] // 子级绑定父级
} else if (type === 'ancestor') {
// 当前匹配失败,但type为ancestor时,继续向上查找
return this.__mpxCheckParent(target, relation, path)
Expand All @@ -230,13 +238,28 @@ export default function relationsMixin (mixinType) {
__mpxExecRelations (type) {
Object.keys(this.__mpxRelations).forEach(path => {
const { target, targetRelation, relation } = this.__mpxRelations[path]
const currentPath = this.$options.componentPath
if (type === 'linked') {
this.__mpxLinkRelationNodes(target, currentPath)
} else if (type === 'unlinked') {
this.__mpxRemoveRelationNodes(target, currentPath)
}
if (typeof targetRelation[type] === 'function') {
targetRelation[type].call(target, this)
}
if (typeof relation[type] === 'function') {
relation[type].call(this, target)
}
})
},
__mpxLinkRelationNodes (target, path) {
target.__mpxRelationNodesMap[path] = target.__mpxRelationNodesMap[path] || [] // 父级绑定子级
target.__mpxRelationNodesMap[path].push(this)
},
__mpxRemoveRelationNodes (target, path) {
const arr = target.__mpxRelationNodesMap[path] || []
const index = arr.indexOf(this)
if (index !== -1) arr.splice(index, 1)
}
}
}
Expand Down

0 comments on commit aa0478d

Please sign in to comment.