Skip to content

Commit

Permalink
Merge pull request nuxt#1914 from dotneet/fix-1408
Browse files Browse the repository at this point in the history
fix unexpected state resetting nuxt#1408
  • Loading branch information
Atinux committed Oct 31, 2017
2 parents 64285cd + f186cad commit e15c92f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/app/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
sanitizeComponent,
resolveRouteComponents,
getMatchedComponents,
getMatchedComponentsInstances,
getChangedComponentsInstances,
flatMapComponents,
setContext,
middlewareSeries,
Expand Down Expand Up @@ -346,16 +346,17 @@ function showNextPage(to) {

// When navigating on a different route but the same component is used, Vue.js
// Will not update the instance data, so we have to update $data ourselves
function fixPrepatch (to, ___) {
function fixPrepatch (to, from) {
if (this._hashChanged) return

Vue.nextTick(() => {
const instances = getMatchedComponentsInstances(to)
const instances = getChangedComponentsInstances(to, from)

var dlen = to.matched.length - instances.length
_lastComponentsFiles = instances.map((instance, i) => {
if (!instance) return '';

if (_lastPaths[i] === instance.constructor._path && typeof instance.constructor.options.data === 'function') {
if (_lastPaths[dlen + i] === instance.constructor._path && typeof instance.constructor.options.data === 'function') {
const newData = instance.constructor.options.data.call(instance)
for (let key in newData) {
Vue.set(instance.$data, key, newData[key])
Expand Down
42 changes: 42 additions & 0 deletions lib/app/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,48 @@ export function getMatchedComponentsInstances(route) {
}))
}

function getRouteRecordWithParamNames (route) {
return route.matched.map(m => {
var paramNames = m.path.match(new RegExp(':[^\\/\\?]+', 'g'))
if (paramNames !== null) {
paramNames = paramNames.map(function (name) {
return name.substring(1)
})
}
return {
routeRecord: m,
paramNames: paramNames
}
})
}

export function getChangedComponentsInstances (to, from) {
var records = getRouteRecordWithParamNames(to)
var r = []
var parentChange = false
for (var i = 0; i < records.length; i++ ) {
var paramNames = records[i].paramNames
var instances = records[i].routeRecord.instances
instances = Object.keys(instances).map(function (key) {
return instances[key]
})
if (parentChange) {
r = [].concat(r, instances)
} else if (paramNames !== null) {
for (var pi in paramNames) {
var name = paramNames[pi]
if (to.params[name] !== from.params[name]) {
parentChange = true
r = [].concat(r, instances)
break
}
}
}
}
return r
}


export function flatMapComponents(route, fn) {
return Array.prototype.concat.apply([], route.matched.map(function (m, index) {
return Object.keys(m.components).map(function (key) {
Expand Down

0 comments on commit e15c92f

Please sign in to comment.