Skip to content

Commit

Permalink
fix scope resolution closes #699
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Aug 3, 2017
1 parent 4867d22 commit b6a2fd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const createDirective = options => {
const fieldOptions = Generator.generate(el, binding, vnode, options);
validator.attach(fieldOptions);
},
inserted (el, { value, expression }, { context }) {
const field = findField(el, context);
inserted (el, binding, vnode) {
const field = findField(el, vnode.context);
if (!field) return;

const scope = isObject(value) && value.rules ? value.scope : getScope(el);
const scope = Generator.resolveScope(el, binding, vnode);
field.update({ scope });
},
update (el, { expression, value }, { context }) {
Expand Down
15 changes: 12 additions & 3 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Generator {
name: Generator.resolveName(el, vnode),
el: el,
listen: !binding.modifiers.disable,
scope: Generator.resolveScope(el, binding),
scope: Generator.resolveScope(el, binding, vnode),
vm: vnode.context,
expression: binding.value,
component: vnode.child,
Expand Down Expand Up @@ -65,8 +65,17 @@ export default class Generator {
* @param {*} el
* @param {*} binding
*/
static resolveScope (el, binding) {
return (isObject(binding.value) ? binding.value.scope : getScope(el));
static resolveScope (el, binding, vnode = {}) {
let scope = null;
if (isObject(binding.value)) {
scope = binding.value.scope;
}

if (vnode.child && !scope) {
scope = vnode.child.$attrs && vnode.child.$attrs['data-vv-scope'];
}

return scope || getScope(el);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion tests/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('resolves the input scope', () => {
<input type="text" name="field" id="el" v-model="email" data-vv-scope="s1">
`;
let el = document.querySelector('#el');
const vnode = { context: { email: '' }, data: { model: { expression: 'email' } } };
let vnode = { context: { email: '' }, data: { model: { expression: 'email' } } };
expect(Generator.resolveScope(el, { arg: null, value: null })).toBe('s1');

// defined on form.
Expand All @@ -52,6 +52,15 @@ test('resolves the input scope', () => {
// defined in expression.
el = document.querySelector('#el');
expect(Generator.resolveScope(el, { arg: null, value: { scope: 's3'} })).toBe('s3');

// defined in a components $attrs.
el = document.querySelector('#el');
vnode = { child: { $attrs: { 'data-vv-scope': 's4' } } };
expect(Generator.resolveScope(el, { arg: null, value: null }, vnode)).toBe('s4');

// define in a component's $el
vnode = { child: { $attrs: {} } };
expect(Generator.resolveScope(el, { arg: null, value: null }, vnode)).toBe('s2');
});

test('resolves delay', () => {
Expand Down

0 comments on commit b6a2fd4

Please sign in to comment.