From 74b16318bcca25942458f41e80df70db58331535 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sun, 17 Jan 2016 03:28:21 +0900 Subject: [PATCH] feat(custom): support local registration Closes #125 --- src/asset.js | 17 +++++++++---- test/specs/asset.js | 58 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/asset.js b/src/asset.js index 37a4498..ea3fc53 100644 --- a/src/asset.js +++ b/src/asset.js @@ -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 + } } /** diff --git a/test/specs/asset.js b/test/specs/asset.js index 6e6ed2e..95f3b21 100644 --- a/test/specs/asset.js +++ b/test/specs/asset.js @@ -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 = '' + + '
' + + '' + + '
' + + '
' + 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) + }) }) })