-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: v-if/v-else-if/v-else and v-model updating the wrong model
- Loading branch information
Showing
5 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
import { genComponentModel, genAssignmentCode } from 'compiler/directives/model' | ||
import { isKnownView, getViewMeta } from '../../element-registry' | ||
import { addHandler, addAttr } from 'compiler/helpers' | ||
|
||
export default function model(el, dir) { | ||
if (el.type === 1 && isKnownView(el.tag)) { | ||
genDefaultModel(el, dir.value, dir.modifiers) | ||
genViewComponentModel(el, dir.value, dir.modifiers) | ||
} else { | ||
genComponentModel(el, dir.value, dir.modifiers) | ||
} | ||
} | ||
|
||
function genDefaultModel(el, value, modifiers) { | ||
const { trim, number } = modifiers || {} | ||
const { prop, event } = getViewMeta(el.tag).model | ||
|
||
let valueExpression = `$event.value${trim ? '.trim()' : ''}` | ||
function genViewComponentModel(el, value, modifiers) { | ||
debugger | ||
const { number, trim } = modifiers || {} | ||
const { prop } = getViewMeta(el.tag).model | ||
|
||
const baseValueExpression = '$event' | ||
let valueExpression = `${baseValueExpression}.object[${JSON.stringify(prop)}]` | ||
if (trim) { | ||
valueExpression = | ||
`(typeof ${valueExpression} === 'string'` + | ||
`? ${valueExpression}.trim()` + | ||
`: ${valueExpression})` | ||
} | ||
if (number) { | ||
valueExpression = `_n(${valueExpression})` | ||
} | ||
const assignment = genAssignmentCode(value, valueExpression) | ||
|
||
const code = genAssignmentCode(value, valueExpression) | ||
|
||
addAttr(el, prop, `(${value})`) | ||
addHandler(el, event, code, null, true) | ||
el.model = { | ||
value: `(${value})`, | ||
expression: JSON.stringify(value), | ||
callback: `function (${baseValueExpression}) {debugger;${assignment}}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const Vue = require('nativescript-vue') | ||
|
||
Vue.config.debug = true | ||
Vue.config.silent = false | ||
|
||
// console.log(Vue.compile('<Switch v-model="arrival" color="red"/>').render.toString()) | ||
|
||
new Vue({ | ||
data() { | ||
return { | ||
items: [ | ||
{ switch: false, value: 'Item 1' }, | ||
{ switch: false, value: 'Item 2' }, | ||
{ switch: false, value: 'Item 3' }, | ||
{ switch: false, value: 'Item 4' }, | ||
{ switch: false, value: 'Item 5' }, | ||
{ switch: false, value: 'Item 6' }, | ||
{ switch: false, value: 'Item 7' }, | ||
{ switch: false, value: 'Item 8' }, | ||
{ switch: false, value: 'Item 9' }, | ||
{ switch: false, value: 'Item 10' }, | ||
{ switch: false, value: 'Item 11' }, | ||
{ switch: false, value: 'Item 12' }, | ||
{ switch: false, value: 'Item 13' }, | ||
{ switch: false, value: 'Item 14' }, | ||
{ switch: false, value: 'Item 15' }, | ||
{ switch: false, value: 'Item 16' }, | ||
{ switch: false, value: 'Item 17' }, | ||
{ switch: false, value: 'Item 18' }, | ||
{ switch: false, value: 'Item 19' }, | ||
{ switch: false, value: 'Item 20' } | ||
] | ||
} | ||
}, | ||
template: ` | ||
<ListView for="item in items"> | ||
<v-template> | ||
<GridLayout padding="24" columns="*, auto, auto"> | ||
<TextField v-model="item.value"/> | ||
<Label :text="item.switch" col="1"/> | ||
<Switch v-model="item.switch" col="2"/> | ||
</GridLayout> | ||
</v-template> | ||
</ListView> | ||
` | ||
}).$start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const Vue = require('nativescript-vue') | ||
|
||
Vue.config.debug = true | ||
Vue.config.silent = false | ||
|
||
// console.log(Vue.compile('<Switch v-model="arrival" color="red"/>').render.toString()) | ||
|
||
new Vue({ | ||
data() { | ||
return { | ||
currentStep: 0, | ||
departure: false, | ||
arrival: false | ||
} | ||
}, | ||
template: ` | ||
<StackLayout> | ||
<Button :text="\`Step 0 - \${departure}\`" :color="currentStep === 0 ? 'green' : 'black'" @tap="currentStep = 0"/> | ||
<Button :text="\`Step 1 - \${arrival}\`" :color="currentStep === 1 ? 'green' : 'black'" @tap="currentStep = 1"/> | ||
<Button text="Toggle" @tap="departure = !departure"/> | ||
<GridLayout v-if="currentStep===0"> | ||
<Switch v-model="departure" color="blue"/> | ||
</GridLayout> | ||
<GridLayout v-else-if="currentStep===1"> | ||
<Switch v-model="arrival" color="red"/> | ||
</GridLayout> | ||
</StackLayout> | ||
` | ||
}).$start() |