Skip to content
This repository has been archived by the owner on Dec 25, 2017. It is now read-only.

Commit

Permalink
feat(custom): support local registration
Browse files Browse the repository at this point in the history
Closes #125
  • Loading branch information
kazupon committed Jan 16, 2016
1 parent 7c00227 commit 74b1631
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
17 changes: 12 additions & 5 deletions src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ import * as validators from './validators'


export default function (Vue) {

// register validator asset
Vue.config._assetTypes.push('validator')
const extend = Vue.util.extend

// set global validators asset
let assets = Object.create(null)
Vue.util.extend(assets, validators)
extend(assets, validators)
Vue.options.validators = assets

// set option merge strategy
let strats = Vue.config.optionMergeStrategies
if (strats) {
strats.validators = strats.methods
strats.validators = (parent, child) => {
if (!child) { return parent }
if (!parent) { return child }
const ret = Object.create(null)
extend(ret, parent)
for (let key in child) {
ret[key] = child[key]
}
return ret
}
}

/**
Expand Down
58 changes: 54 additions & 4 deletions test/specs/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,60 @@ describe('asset', () => {
})

describe('custom validator', () => {
it('should be registered', () => {
let validator = (val, ...args) => { return false }
Vue.validator('custom', validator)
assert(Vue.validator('custom') === validator)
context('global', () => {
it('should be registered', () => {
let validator = (val, ...args) => { return false }
Vue.validator('custom', validator)
assert(Vue.validator('custom') === validator)
})
})

context('local', () => {
let el1
let el2
let globalCount = 0
let localCount = 0
beforeEach((done) => {
let template = '<validator name="validator1">' +
'<form novalidate>' +
'<input type="text" v-validate:field1="{ custom11: true }">' +
'</form>' +
'</validator>'
el1 = document.createElement('div')
el1.innerHTML = template
el2 = document.createElement('div')
el2.innerHTML = template
let global = (val, ...args) => {
globalCount++
return false
}
Vue.validator('custom11', global)
let local = (val, ...args) => {
localCount++
return true
}
let vm1 = new Vue({
el: el1
})
vm1.$nextTick(() => {
let vm2 = new Vue({
el: el2,
validators: { custom11: local }
})
vm2.$nextTick(() => {
let el3 = document.createElement('div')
el3.innerHTML = template
new Vue({
el: el3
})
done()
})
})
})

it('should be registered', () => {
assert(globalCount > localCount)
})
})
})

Expand Down

0 comments on commit 74b1631

Please sign in to comment.