Skip to content

Commit f40e2d7

Browse files
committed
use bind- syntax for props
1 parent c352b57 commit f40e2d7

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

src/compiler/compile-props.js

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,12 @@ module.exports = function compileProps (el, propOptions) {
4949
attr = _.hyphenate(name)
5050
value = el.getAttribute(attr)
5151

52-
if (value !== null && process.env.NODE_ENV !== 'production') {
53-
_.deprecation.PROPS(attr, value)
54-
}
55-
5652
if (value === null) {
5753
value = el.getAttribute('data-' + attr)
5854
if (value !== null) {
5955
attr = 'data-' + attr
6056
if (process.env.NODE_ENV !== 'production') {
61-
_.deprecation.PROPS(attr, value)
57+
_.deprecation.DATA_PROPS(attr, value)
6258
}
6359
}
6460
}
@@ -77,6 +73,11 @@ module.exports = function compileProps (el, propOptions) {
7773
el.removeAttribute(attr)
7874
var tokens = textParser.parse(value)
7975
if (tokens) {
76+
77+
if (process.env.NODE_ENV !== 'production') {
78+
_.deprecation.PROPS(attr, value)
79+
}
80+
8081
prop.dynamic = true
8182
prop.parentPath = textParser.tokensToExp(tokens)
8283
// check prop binding type.
@@ -100,30 +101,35 @@ module.exports = function compileProps (el, propOptions) {
100101
}
101102
}
102103
} else {
103-
// new prop- syntax
104-
attr = 'prop-' + attr
104+
// new syntax
105+
attr = 'bind-' + attr
105106
value = prop.raw = el.getAttribute(attr)
106107
if (value !== null) {
108+
// mark it so we know this is a bind
109+
prop.bindSyntax = true
107110
el.removeAttribute(attr)
111+
value = value.trim()
108112
// check binding type
109113
if (literalValueRE.test(value)) {
110114
prop.mode = propBindingModes.ONE_TIME
111-
} else if (value.charAt(0) === '*') {
112-
prop.mode = propBindingModes.ONE_TIME
113-
value = value.slice(1)
114-
} else if (value.charAt(0) === '@') {
115-
value = value.slice(1)
116-
if (settablePathRE.test(value)) {
117-
prop.mode = propBindingModes.TWO_WAY
118-
} else {
119-
process.env.NODE_ENV !== 'production' && _.warn(
120-
'Cannot bind two-way prop with non-settable ' +
121-
'parent path: ' + value
122-
)
115+
} else {
116+
prop.dynamic = true
117+
if (value.charAt(0) === '*') {
118+
prop.mode = propBindingModes.ONE_TIME
119+
value = value.slice(1).trim()
120+
} else if (value.charAt(0) === '@') {
121+
value = value.slice(1).trim()
122+
if (settablePathRE.test(value)) {
123+
prop.mode = propBindingModes.TWO_WAY
124+
} else {
125+
process.env.NODE_ENV !== 'production' && _.warn(
126+
'Cannot bind two-way prop with non-settable ' +
127+
'parent path: ' + value
128+
)
129+
}
123130
}
124131
}
125132
}
126-
prop.dynamic = true
127133
prop.parentPath = value
128134
}
129135

@@ -193,13 +199,18 @@ function makePropsLinkFn (props) {
193199
} else {
194200
// literal, cast it and just set once
195201
var raw = prop.raw
196-
value = options.type === Boolean && raw === ''
197-
? true
198-
// do not cast emptry string.
199-
// _.toNumber casts empty string to 0.
200-
: raw.trim()
201-
? _.toBoolean(_.toNumber(raw))
202-
: raw
202+
if (options.type === Boolean && raw === '') {
203+
value = true
204+
} else if (raw.trim()) {
205+
value = _.toBoolean(_.toNumber(raw))
206+
if (process.env.NODE_ENV !== 'production' &&
207+
!prop.bindSyntax &&
208+
value !== raw) {
209+
_.deprecation.PROP_CASTING(prop.name, prop.raw)
210+
}
211+
} else {
212+
value = raw
213+
}
203214
_.initProp(vm, prop, value)
204215
}
205216
}

src/deprecations.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,24 @@ if (process.env.NODE_ENV !== 'production') {
134134

135135
PROPS: function (attr, value) {
136136
warn(
137-
'Prop ' + attr + '="' + value + '" should be prefixed with "prop-" and ' +
138-
'bound as expression in 1.0.0. ' +
139-
'For more details, see https://github.com/yyx990803/vue/issues/1173'
137+
'Prop ' + attr + '="' + value + '": props no longer use mustache tags ' +
138+
'to indicate a dynamic binding. Use the "bind-" prefix instead.' +
139+
newBindingSyntaxLink
140+
)
141+
},
142+
143+
DATA_PROPS: function (attr, value) {
144+
warn(
145+
'Prop ' + attr + '="' + value + '": props will no longer support the ' +
146+
'"data-" prefix in 1.0.0.' + newBindingSyntaxLink
147+
)
148+
},
149+
150+
PROP_CASTING: function (attr, value) {
151+
warn(
152+
'Prop ' + attr + '="' + value + '": literal props will no longer be ' +
153+
'auto-casted into booleans/numbers in 1.0.0 - they will all be treated ' +
154+
'as literal strings. Use "bind-" syntax for boolean/number literals instead.'
140155
)
141156
},
142157

test/unit/specs/compiler/compile_spec.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,18 +305,22 @@ if (_.inBrowser) {
305305
{ name: 'testLiteral' },
306306
{ name: 'testTwoWay' },
307307
{ name: 'twoWayWarn' },
308-
{ name: 'testOneTime' }
308+
{ name: 'testOneTime' },
309+
{ name: 'optimizeLiteral' }
309310
]
310-
el.setAttribute('prop-test-normal', 'a')
311-
el.setAttribute('prop-test-literal', '1')
312-
el.setAttribute('prop-test-two-way', '@a')
313-
el.setAttribute('prop-two-way-warn', '@a + 1')
314-
el.setAttribute('prop-test-one-time', '*a')
311+
el.setAttribute('bind-test-normal', 'a')
312+
el.setAttribute('test-literal', '1')
313+
el.setAttribute('bind-optimize-literal', '1')
314+
el.setAttribute('bind-test-two-way', '@a')
315+
el.setAttribute('bind-two-way-warn', '@a + 1')
316+
el.setAttribute('bind-test-one-time', '*a')
315317
compiler.compileAndLinkProps(vm, el, props)
316318
expect(vm._bindDir.calls.count()).toBe(3) // skip literal and one time
317319
// literal
318-
expect(vm.testLiteral).toBe('from parent: 1')
319-
expect(vm._data.testLiteral).toBe('from parent: 1')
320+
expect(vm.testLiteral).toBe(1)
321+
expect(vm._data.testLiteral).toBe(1)
322+
expect(vm.optimizeLiteral).toBe(1)
323+
expect(vm._data.optimizeLiteral).toBe(1)
320324
// one time
321325
expect(vm.testOneTime).toBe('from parent: a')
322326
expect(vm._data.testOneTime).toBe('from parent: a')

0 commit comments

Comments
 (0)