From 4fa1c133f3ce9bd9b25c3ba352316db67d26bfe6 Mon Sep 17 00:00:00 2001 From: Declan de Wet Date: Sun, 30 Oct 2016 05:59:48 +0200 Subject: [PATCH] allow passing props & data by specifying metaInfo prop as a function --- index.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9da9ca8..8e03790 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,7 @@ * Updates meta info and renders it to the DOM */ Vue.prototype.$updateMeta = function $updateMeta () { - const newMeta = this.$meta() + var newMeta = this.$meta() document.title = newMeta.title } @@ -59,9 +59,22 @@ function getMetaInfoDefinition (Vue, $instance, metaInfo) { // set default for first run metaInfo = metaInfo || {} - // if current instance has a metaInfo option, merge it in + // if current instance has a metaInfo option... if ($instance.$options.metaInfo) { - metaInfo = Vue.util.mergeOptions(metaInfo, $instance.$options.metaInfo) + var componentMetaInfo = $instance.$options.metaInfo + var key + // ...convert all function type keys to raw data + // (this allows meta info to be inferred from props & data)... + for (key in componentMetaInfo) { + if (componentMetaInfo.hasOwnProperty(key)) { + var val = componentMetaInfo[key] + if (typeof val === 'function') { + componentMetaInfo[key] = val.call($instance) + } + } + } + // ...then merge the data into metaInfo + metaInfo = Vue.util.mergeOptions(metaInfo, componentMetaInfo) } // check if any children also have a metaInfo option, if so, merge // them into existing data