From e2ad6517e2020d3b230491d87a57286ae3f166d1 Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Fri, 14 Apr 2017 16:31:14 +0200 Subject: [PATCH] Give this to data() --- lib/app/client.js | 35 ++++++----------------------------- lib/app/server.js | 13 +++---------- lib/app/utils.js | 13 +++++++++++++ 3 files changed, 22 insertions(+), 39 deletions(-) diff --git a/lib/app/client.js b/lib/app/client.js index 589b62617a..3627902d87 100644 --- a/lib/app/client.js +++ b/lib/app/client.js @@ -3,7 +3,7 @@ import Vue from 'vue' import middleware from './middleware' import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index' -import { getMatchedComponents, getMatchedComponentsInstances, flatMapComponents, getContext, promiseSeries, promisify, getLocation, compile } from './utils' +import { applyAsyncData, getMatchedComponents, getMatchedComponentsInstances, flatMapComponents, getContext, promiseSeries, promisify, getLocation, compile } from './utils' const noopData = () => { return {} } const noopFetch = () => {} let _lastPaths = [] @@ -148,18 +148,7 @@ function render (to, from, next) { if (Component.options.asyncData && typeof Component.options.asyncData === 'function') { var promise = promisify(Component.options.asyncData, context) promise.then((asyncDataResult) => { - let data = {} - // Call data() if defined - if (Component.options.data && typeof Component.options.data === 'function') { - data = Component.options.data() - } - // Merge data() and asyncData() results - data = Object.assign(data, asyncDataResult) - // Overwrite .data() method with merged data - Component.options.data = () => data - if (Component._Ctor && Component._Ctor.options) { - Component._Ctor.options.data = Component.options.data - } + applyAsyncData(Component, asyncDataResult) <%= (loading ? 'this.$loading.increase && this.$loading.increase(30)' : '') %> }) promises.push(promise) @@ -218,7 +207,7 @@ function fixPrepatch (to, ___) { _lastComponentsFiles = instances.map((instance, i) => { if (!instance) return ''; if (_lastPaths[i] === instance.constructor._path && typeof instance.constructor.options.data === 'function') { - let newData = instance.constructor.options.data() + let newData = instance.constructor.options.data.call(instance) for (let key in newData) { Vue.set(instance.$data, key, newData[key]) } @@ -305,13 +294,10 @@ function addHotReload ($component, depth) { return callMiddleware.call(this, Components, context, this.layout) }) .then(() => { - // Call asyncData() + // Call asyncData(context) let pAsyncData = promisify(Component.options.asyncData || noopData, context) pAsyncData.then((asyncDataResult) => { - let data = (typeof Component.options.data === 'function' ? Component.options.data() : noopData()) - data = Object.assign(data, asyncDataResult) - Component.options.data = () => data - Component._Ctor.options.data = Component.options.data + applyAsyncData(Component, asyncDataResult) <%= (loading ? 'this.$loading.increase && this.$loading.increase(30)' : '') %> }) promises.push(pAsyncData) @@ -350,16 +336,7 @@ const resolveComponents = flatMapComponents(router.match(path), (Component, _, m Component.extendOptions = Component.options } if (NUXT.serverRendered) { - let data = {} - if (Component.options.data && typeof Component.options.data === 'function') { - data = Component.options.data() - } - // Merge data() and asyncData() results - data = Object.assign(data, NUXT.data[index]) - Component.options.data = () => data - if (Component._Ctor && Component._Ctor.options) { - Component._Ctor.options.data = Component.options.data - } + applyAsyncData(Component, NUXT.data[index]) } match.components[key] = Component resolve(Component) diff --git a/lib/app/server.js b/lib/app/server.js index 2ce018ac35..b0e0d62db8 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -8,7 +8,7 @@ import { stringify } from 'querystring' import { omit } from 'lodash' import middleware from './middleware' import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index' -import { getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils' +import { applyAsyncData, getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils' const isDev = <%= isDev %> const _app = new Vue(app) @@ -172,15 +172,8 @@ export default context => { let promise = promisify(Component.options.asyncData, ctx) // Call asyncData(context) promise.then((asyncDataResult) => { - let data = {} - // Call data() if defined - if (Component.options.data && typeof Component.options.data === 'function') { - data = Component.options.data() - } - // Merge data() and asyncData() results - data = Object.assign(data, asyncDataResult) - Component.options.data = () => data - Component._Ctor.options.data = Component.options.data + applyAsyncData(Component, asyncDataResult) + return asyncDataResult }) promises.push(promise) } else { diff --git a/lib/app/utils.js b/lib/app/utils.js index 934ccedc33..8af0355682 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -1,6 +1,19 @@ 'use strict' import { app } from './index' +const noopData = () => ({}) + +export function applyAsyncData (Component, asyncData = {}) { + const ComponentData = Component.options.data || noopData + Component.options.data = function () { + const data = ComponentData.call(this) + return { ...data, ...asyncData } + } + if (Component._Ctor && Component._Ctor.options) { + Component._Ctor.options.data = Component.options.data + } +} + export function getMatchedComponents (route) { return [].concat.apply([], route.matched.map(function (m) { return Object.keys(m.components).map(function (key) {