@@ -37,42 +37,57 @@ export function genAssignmentCode (
37
37
value: string,
38
38
assignment: string
39
39
): string {
40
-
const modelRs = parseModel(value)
41
-
if (modelRs.idx === null) {
40
+
const res = parseModel(value)
41
+
if (res.key === null) {
42
42
return `${value}=${assignment}`
43
43
} else {
44
-
return `$set(${modelRs.exp}, ${modelRs.idx}, ${assignment})`
44
+
return `$set(${res.exp}, ${res.key}, ${assignment})`
45
45
}
46
46
}
47
47
48
48
/**
49
-
* parse directive model to do the array update transform. a[idx] = val => $$a.splice($$idx, 1, val)
49
+
* Parse a v-model expression into a base path and a final key segment.
50
+
* Handles both dot-path and possible square brackets.
50
51
*
51
-
* for loop possible cases:
52
+
* Possible cases:
52
53
*
53
54
* - test
54
-
* - test[idx]
55
-
* - test[test1[idx]]
56
-
* - test["a"][idx]
57
-
* - xxx.test[a[a].test1[idx]]
58
-
* - test.xxx.a["asa"][test1[idx]]
55
+
* - test[key]
56
+
* - test[test1[key]]
57
+
* - test["a"][key]
58
+
* - xxx.test[a[a].test1[key]]
59
+
* - test.xxx.a["asa"][test1[key]]
59
60
*
60
61
*/
61
62
62
63
let len, str, chr, index, expressionPos, expressionEndPos
63
64
64
-
export function parseModel (val: string): Object {
65
-
str = val
66
-
len = str.length
67
-
index = expressionPos = expressionEndPos = 0
65
+
type ModelParseResult = {
66
+
exp: string,
67
+
key: string | null
68
+
}
69
+
70
+
export function parseModel (val: string): ModelParseResult {
71
+
len = val.length
68
72
69
73
if (val.indexOf('[') < 0 || val.lastIndexOf(']') < len - 1) {
70
-
return {
71
-
exp: val,
72
-
idx: null
74
+
index = val.lastIndexOf('.')
75
+
if (index > -1) {
76
+
return {
77
+
exp: val.slice(0, index),
78
+
key: '"' + val.slice(index + 1) + '"'
79
+
}
80
+
} else {
81
+
return {
82
+
exp: val,
83
+
key: null
84
+
}
73
85
}
74
86
}
75
87
88
+
str = val
89
+
index = expressionPos = expressionEndPos = 0
90
+
76
91
while (!eof()) {
77
92
chr = next()
78
93
/* istanbul ignore if */
@@ -84,8 +99,8 @@ export function parseModel (val: string): Object {
84
99
}
85
100
86
101
return {
87
-
exp: val.substring(0, expressionPos),
88
-
idx: val.substring(expressionPos + 1, expressionEndPos)
102
+
exp: val.slice(0, expressionPos),
103
+
key: val.slice(expressionPos + 1, expressionEndPos)
89
104
}
90
105
}
91
106
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4