Skip to content

Commit

Permalink
支持lifetimes & pageLifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
pkingwa committed Jan 4, 2019
1 parent f8c2484 commit 155f636
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions packages/core/src/core/mergeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,19 @@ function extractMixins (mergeOptions, options) {
extractMixins(mergeOptions, mix)
}
}
mergeMixins(mergeOptions, extractPageHooks(options))
options = extractLifetimes(options)
options = extractPageHooks(options)
mergeMixins(mergeOptions, options)
}

function extractLifetimes (options) {
if (type(options.lifetimes) === 'Object') {
const newOptions = extend({}, options, options.lifetimes)
delete newOptions.lifetimes
return newOptions
} else {
return options
}
}

function extractPageHooks (options) {
Expand Down Expand Up @@ -56,8 +68,8 @@ function mergeMixins (parent, child) {
mergeData(parent, child, key)
} else if (/computed|properties|methods|proto/.test(key)) {
mergeSimpleProps(parent, child, key)
} else if (key === 'watch') {
mergeWatch(parent, child, key)
} else if (/watch|pageLifetimes/.test(key)) {
mergeToArray(parent, child, key)
} else if (key !== 'mixins') {
mergeDefault(parent, child, key)
}
Expand All @@ -69,7 +81,7 @@ function mergeDefault (parent, child, key) {
}

function mergeHooks (parent, child, key) {
if (parent.hasOwnProperty(key)) {
if (parent[key]) {
parent[key].push(child[key])
} else {
parent[key] = [child[key]]
Expand All @@ -93,7 +105,7 @@ function mergeData (parent, child, key) {
merge(parent[key], childVal)
}

function mergeWatch (parent, child, key) {
function mergeToArray (parent, child, key) {
let parentVal = parent[key]
const childVal = child[key]
if (!parentVal) {
Expand All @@ -105,25 +117,32 @@ function mergeWatch (parent, child, key) {
? [parentVal[key], childVal[key]]
: parentVal[key].concat([childVal[key]])
} else {
parentVal[key] = childVal[key]
parentVal[key] = [childVal[key]]
}
})
}

function transformHOOKS (options) {
CURRENT_HOOKS.forEach(key => {
const hooksArr = options[key]
hooksArr && (options[key] = function (...args) {
let result
for (let i = 0; i < hooksArr.length; i++) {
if (type(hooksArr[i]) === 'Function') {
const data = hooksArr[i].apply(this, args)
data !== undefined && (result = data)
function composeHooks (target, includes) {
Object.keys(target).forEach(key => {
if (!includes || includes.indexOf(key) !== -1) {
const hooksArr = target[key]
hooksArr && (target[key] = function (...args) {
let result
for (let i = 0; i < hooksArr.length; i++) {
if (type(hooksArr[i]) === 'Function') {
const data = hooksArr[i].apply(this, args)
data !== undefined && (result = data)
}
}
}
return result
})
return result
})
}
})
}

function transformHOOKS (options) {
composeHooks(options, CURRENT_HOOKS)
options.pageLifetimes && composeHooks(options.pageLifetimes)
if (curType === 'blend') {
for (const key in options) {
// 使用Component创建page实例,页面专属生命周期&自定义方法需写在methods内部
Expand Down

0 comments on commit 155f636

Please sign in to comment.