2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-09 13:22:27 +03:00

add tests for getComponentOption

This commit is contained in:
Declan de Wet
2016-11-04 05:04:34 +02:00
parent a17478c2ae
commit 70a1578e7f
2 changed files with 82 additions and 19 deletions
+23 -19
View File
@@ -10,6 +10,7 @@ import deepmerge from 'deepmerge'
* @param {Object} opts.component - Vue component to fetch option data from
* @param {String} opts.option - what option to look for
* @param {Boolean} opts.deep - look for data in child components as well?
* @param {Function} opts.arrayMerge - how should arrays be merged?
* @param {Object} [result={}] - result so far
* @return {Object} - final aggregated result
*/
@@ -18,32 +19,35 @@ export default function getComponentOption (opts, result = {}) {
const { $options } = component
// only collect option data if it exists
if ($options[option]) {
if (typeof $options[option] !== 'undefined' && $options[option] !== null) {
const data = $options[option]
// TODO: check data is plain object, throw if not
// bind context of option methods (if any) to this component
for (const key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key]
if (typeof value === 'function') {
data[key] = value.bind(component)
if (typeof data === 'object') {
// bind context of option methods (if any) to this component
for (const key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key]
if (typeof value === 'function') {
data[key] = value.bind(component)
}
}
}
}
// merge with existing options
result = deepmerge(result, data, { arrayMerge })
}
// merge with existing options
result = deepmerge(result, data, { arrayMerge })
// collect & aggregate child options if deep = true
if (deep) {
const { $children } = component
for (let i = 0, len = $children.length; i < len; i++) {
const component = $children[i]
result = getComponentOption({ option, deep, component, arrayMerge }, result)
// collect & aggregate child options if deep = true
if (deep) {
const { $children } = component
for (let i = 0, len = $children.length; i < len; i++) {
const component = $children[i]
result = getComponentOption({ option, deep, component, arrayMerge }, result)
}
}
return result
}
result = data
}
return result