-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(v-model): transformModel should check if prop is defined and fall back to attrs if needed (fix #8430) #8442
Conversation
…back to attrs if needed (issue vuejs#8430)
src/core/vdom/create-component.js
Outdated
@@ -247,7 +247,12 @@ function installComponentHooks (data: VNodeData) { | |||
function transformModel (options, data: any) { | |||
const prop = (options.model && options.model.prop) || 'value' | |||
const event = (options.model && options.model.event) || 'input' | |||
;(data.props || (data.props = {}))[prop] = data.model.value | |||
// check if prop is defined, if not, attrs will be used | |||
if (options.props && options.props[prop]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be refactored to
const propOrAtrrKey = options.props && options.props[prop] ? 'props' : 'attrs'
;(data[propOrAttrKey]|| (data[propOrAttrKey] = {}))[prop] = data.model.value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a unit test for this please?
… back to attrs if needed (fix vuejs#8430)
It is the first time i did a unit test, is this ok ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move the test to model-component.spec.js?
describe('Options model', () => { | ||
it('key should fallback to attrs if prop is not defined in component', () => { | ||
const ChildComp = { | ||
model: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can omit the model option in the component
template: '<child-comp v-model="value" ref="child-comp" />' | ||
}).$mount() | ||
|
||
expect(vm.$refs['child-comp'].$attrs.value).toEqual(vm.value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just name it with a one-word name, you can name it test
instead of child-component
import Vue from 'vue' | ||
|
||
describe('Options model', () => { | ||
it('key should fallback to attrs if prop is not defined in component', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better title: adds value to $attrs if no prop is defined
… back to attrs if needed (fix vuejs#8430)
I think, this has been resolved with #9330, which has the same changes as mine ? |
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
Other information: