-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Is there any opportunities to share own methods in a custom directive around bind/unbind/update? #314
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
Comments
Why not emit an event on the VM? Vue.directive('xxx', {
bind: function () {
this.handler = function () {
this.vm.$emit('xxx:click', 'param1' /* , … */);
}.bind(this);
this.el.addEventListener('click', this.handler);
},
unbind: function () {
this.el.removeEventListener('click', this.handler);
}
}); |
You can just attach the handler to |
@bpierre I just want to set a private handler of the directive, not dispatched abroad. But the same as @yyx990803 's advice that your demo also uses |
Correct me if I'm wrong, but this is no longer a possibility in Vue 2.0 because there is no
which can't be done because the dataset property is a StringMap. |
@yyx990803: I'm facing the same issue as @jasonbodily. |
@jasonbodily @nevcos Do not know if this can meet your need? Vue.directive('xxx', {
bind: function (el) {
const handler = () => { ... }
el.addEventListener('someEvent', handler)
el.$destroy = () => el.removeEventListener('someEvent', handler)
},
unbind: function (el) {
el.$destroy()
}
}); Simply use the |
@HcySunYang Yes, it meets the need. I've actually implemented that way 👍 I was just wondering if it's the best way. |
@HcySunYang It works but looks counter-intuitive :( |
The solution proposed by @HcySunYang looks like some kind of hack. @yyx990803 Can you confirm that this is the intended correct way of achieving this? Or maybe we should not remove event listeners at all? Does Vue's DOM management guarantee that there will be no memory leak issues? |
fix: vuejs#149 恢复对组件 ref 的支持
Summary: 使用 dom dataset 来缓存 以及 vuejs/vue#314 Test Plan: as above Reviewers: #web_reviewers, kuilin Reviewed By: #web_reviewers, kuilin Differential Revision: https://code.yangqianguan.com/D45553
@yyx990803 Any recommendation or advice for cleaning up event listeners with Vue 2.x will be greatly appreciated. |
Normally you don't have to clean up listeners manually. If an element is destroyed (removed from DOM and no longer referenced by javascript), its listeners are also removed (or precisely, no longer referenced by the element thus able to be garbage collected), this is guaranteed by DOM and is not related to vue at all. In most cases if you're not passing references of elements and event listeners around (and accidentally create memory leaks by retaining the references), you don't need to worry about cleaning up event listeners at all. |
@fnlctrl My worry is not my own code, but the actual framework code. "In most cases", sure, but something like Vue is not your usual piece of code. That is, does Vue guarantee the pre-conditions you mentioned which DOM requires in order to remove its listeners? |
Yes. Otherwise it would be a memory-leak bug (which is rare: https://github.com/vuejs/vue/issues?utf8=%E2%9C%93&q=memory+leak+label%3Abug+) and would be fixed. |
For example, I would add a event listener when bind and remove it when unbind. So I wrote:
But the
handler
is exposed out tovm
.Is there any better way?
Thanks.
The text was updated successfully, but these errors were encountered: