Skip to content
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

add coerce option for component props #1955

Merged
merged 2 commits into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/directives/internal/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import Watcher from '../../watcher'
import config from '../../config'
import { assertProp, initProp } from '../../util/index'
import { assertProp, initProp, coerceProp } from '../../util/index'

const bindingModes = config._propBindingModes

Expand All @@ -25,6 +25,7 @@ export default {
parent,
parentKey,
function (val) {
val = coerceProp(prop, val)
if (assertProp(prop, val)) {
child[childKey] = val
}
Expand Down
18 changes: 18 additions & 0 deletions src/util/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function getIsBinding (el) {

export function initProp (vm, prop, value) {
const key = prop.path
value = coerceProp(prop, value)
vm[key] = vm._data[key] = assertProp(prop, value)
? value
: undefined
Expand Down Expand Up @@ -143,6 +144,23 @@ export function assertProp (prop, value) {
return true
}

/**
* Force parsing value with coerce option.
*
* @param {*} value
* @param {Object} options
* @return {*}
*/

export function coerceProp (prop, value) {
var coerce = prop.options.coerce
if (!coerce) {
return value
}
// coerce is a function
return coerce(value)
}

function formatType (val) {
return val
? val.charAt(0).toUpperCase() + val.slice(1)
Expand Down
16 changes: 14 additions & 2 deletions test/unit/specs/directives/internal/prop_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ describe('prop', function () {

describe('assertions', function () {

function makeInstance (value, type, validator) {
function makeInstance (value, type, validator, coerce) {
return new Vue({
el: document.createElement('div'),
template: '<test :test="val"></test>',
Expand All @@ -307,7 +307,8 @@ describe('prop', function () {
props: {
test: {
type: type,
validator: validator
validator: validator,
coerce: coerce
}
}
}
Expand Down Expand Up @@ -391,6 +392,17 @@ describe('prop', function () {
expect(hasWarned('Expected String')).toBe(true)
})

it('type check + coerce', function () {
makeInstance(123, String, null, String)
expect(getWarnCount()).toBe(0)
makeInstance('123', Number, null, Number)
expect(getWarnCount()).toBe(0)
makeInstance('123', Boolean, null, function (val) {
return val === '123'
})
expect(getWarnCount()).toBe(0)
})

it('required', function () {
new Vue({
el: document.createElement('div'),
Expand Down