Skip to content

Commit a953db5

Browse files
msaelicesrigor789
authored andcommitted
fix: re-adding <keep-alive> elements to view (nativescript-vue#291)
* chore: more useful tracing information related to the VDom operations * fix: reset node next sibling when removing it to be able to add the components again. Fix nativescript-vue#220 * test: cleaner sample app which test the nativescript-vue#220 issue * fix: reset the prevSibling of the removed childNode
1 parent 980ee35 commit a953db5

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

__tests__/renderer/ViewNode.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ describe('ViewNode', () => {
241241
expect(prevChildNode.nextSibling).toEqual(nextChildNode)
242242
expect(nextChildNode.prevSibling).toEqual(prevChildNode)
243243
expect(childNode.parentNode).toBeNull()
244+
expect(childNode.prevSibling).toBeNull()
245+
expect(childNode.nextSibling).toBeNull()
244246
})
245247

246248
test('nativeView can be set once', () => {

platform/nativescript/renderer/ViewNode.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ export default class ViewNode {
246246
childNode.nextSibling.prevSibling = childNode.prevSibling
247247
}
248248

249+
// reset the prevSibling and nextSibling. If not, a keep-alived component will
250+
// still have a filled nextSibling attribute so vue will not
251+
// insert the node again to the parent. See #220
252+
childNode.prevSibling = null
253+
childNode.nextSibling = null
254+
249255
this.childNodes = this.childNodes.filter(node => node !== childNode)
250256

251257
viewUtil.removeChild(this, childNode)

platform/nativescript/runtime/node-ops.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ export function appendChild(node, child) {
4141
}
4242

4343
export function parentNode(node) {
44-
trace(`ParentNode(${node})`)
44+
trace(`ParentNode(${node}) -> ${node.parentNode}`)
4545

4646
return node.parentNode
4747
}
4848

4949
export function nextSibling(node) {
50-
trace(`NextSibling(${node})`)
50+
trace(`NextSibling(${node}) -> ${node.nextSibling}`)
5151

5252
return node.nextSibling
5353
}
5454

5555
export function tagName(elementNode) {
56-
trace(`TagName(${elementNode})`)
56+
trace(`TagName(${elementNode}) -> ${elementNode.tagName}`)
5757

5858
return elementNode.tagName
5959
}

samples/app/220.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ Vue.config.silent = false
55

66
const CompButton = {
77
template: `
8-
<Button @tap="counter++">{{label}}: {{counter}}</Button>
8+
<Button>{{label}}: {{counter}}</Button>
99
`,
1010
name: 'CompButton',
11-
props: ['label'],
12-
data() {
13-
return {
14-
counter: 0
15-
}
11+
props: ['label', 'counter'],
12+
destroyed() {
13+
console.log('Component destroyed. This should not happen')
1614
}
1715
}
1816

@@ -27,8 +25,8 @@ new Vue({
2725
<StackLayout>
2826
<Button @tap="counter++">{{counter}}</Button>
2927
<keep-alive>
30-
<CompButton v-if="counter % 2" key="odd" label="Odd" />
31-
<CompButton v-else key="even" label="Even" />
28+
<CompButton v-if="counter % 2" key="odd" label="Odd" :counter="counter" />
29+
<CompButton v-else key="even" label="Even" :counter="counter" />
3230
</keep-alive>
3331
</StackLayout>
3432
</Page>

0 commit comments

Comments
 (0)