Merge pull request #402 from QingWei-Li/fix/clickoutside

optimize clickoutside
pull/433/head
baiyaaaaa 2016-10-14 23:14:32 +08:00 committed by GitHub
commit 320b116640
1 changed files with 8 additions and 3 deletions

View File

@ -1,5 +1,9 @@
import { on, off } from 'wind-dom/src/event'; import { on } from 'wind-dom/src/event';
const nodeList = [];
on(document, 'click', e => {
nodeList.forEach(node => node[clickoutsideContext].documentHandler(e));
});
/** /**
* v-clickoutside * v-clickoutside
* @desc 点击元素外面才会触发的事件 * @desc 点击元素外面才会触发的事件
@ -12,6 +16,7 @@ const clickoutsideContext = '@@clickoutsideContext';
export default { export default {
bind(el, binding, vnode) { bind(el, binding, vnode) {
const id = nodeList.push(el) - 1;
const documentHandler = function(e) { const documentHandler = function(e) {
if (!vnode.context || if (!vnode.context ||
el.contains(e.target) || el.contains(e.target) ||
@ -24,11 +29,11 @@ export default {
} }
}; };
el[clickoutsideContext] = { el[clickoutsideContext] = {
id,
documentHandler, documentHandler,
methodName: binding.expression, methodName: binding.expression,
bindingFn: binding.value bindingFn: binding.value
}; };
on(document, 'click', documentHandler);
}, },
update(el, binding) { update(el, binding) {
@ -37,7 +42,7 @@ export default {
}, },
unbind(el) { unbind(el) {
off(document, 'click', el[clickoutsideContext].documentHandler); nodeList.splice(el[clickoutsideContext].id, 1);
}, },
install(Vue) { install(Vue) {