Skip to content
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

Widget在dom事件处理上的增强 #14

Closed
lianqin7 opened this issue Oct 11, 2012 · 11 comments
Closed

Widget在dom事件处理上的增强 #14

lianqin7 opened this issue Oct 11, 2012 · 11 comments
Milestone

Comments

@lianqin7
Copy link
Member

@lifesinger

目前在widget内部处理element相关的dom事件都可以通过delegateEvents接口来解决,
但如果是 element之外的dom事件(比如popup中的trigger等) 却没办法通过delegateEvents接口来注册,
用jquery原生的dom事件注册又要考虑 context及unbind 的问题( 这2个问题在delegateEvents中已经解决 ),每次因为这个原因 多写一堆几乎相同的代码 真心蛋疼

建议: 能否提供一对类似于delegateEvents/undelegateEvents的接口来处理非element内部的dom事件 ,对 保持处理逻辑的一致性及widget的易用性 来说都有很大的帮助:)

@lifesinger
Copy link
Member

@lianqin7 这个曾纠结过,但没找到合适的方式来封装

大家有什么好的建议?我的想法:

widgetInstance.bindEvents({
   'selector1 click' : handler1,
   'selector2 focus' : handler2,
})

widgetInstance.unbindEvents(...)

然后 destroy 里也会做相应处理。

相当于一个代理,具体实现还是 jquery。纠结点是,这层代理是否有必要?

@popomore
Copy link
Member

这样只是写法上好看点,感觉是一样的

$(selector1).on('click', handler1);

@popomore
Copy link
Member

是不是可以借鉴 backbone http://backbonejs.org/#Events-listenTo

delegateEvents(element?, events, handler)

这里的 element 是指页面上的任意 DOM,实际上就是帮他绑定,好处是可以统一管理这些事件。

element.on(events, handler)

@lifesinger
Copy link
Member

这个好,可以考虑

On Wed, Mar 27, 2013 at 11:27 AM, Haoliang Gao [email protected]:

是不是可以借鉴 backbone http://backbonejs.org/#Events-listenTo

delegateEvents(element?, events, handler)

这里的 element 是指页面上的任意 DOM,实际上就是帮他绑定,好处是可以统一管理这些事件。

element.on(events, handler)


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-15502394
.

王保平 / 玉伯(射雕)
送人玫瑰手有余香

@lianqin7
Copy link
Member Author

@popomore 是指让delegateEvents多态么?

@popomore
Copy link
Member

@lianqin7 恩,是这个意思

@afc163
Copy link
Member

afc163 commented Apr 2, 2013

目前:

delegateEvents({ key: value })
delegateEvents('click selector', function() {}}

增加:

this.delegateEvents('click selector', function() {}, this.trigger}

@lepture
Copy link
Contributor

lepture commented Apr 2, 2013

delegateEvents('click selector', {
  on: '#trigger',
  do: function() {}
})

@lepture
Copy link
Contributor

lepture commented Apr 2, 2013

delegateEvents(element, 'click selector', function() {})
delegateEvents('click selector', element, function() {})
delegateEvents('click selector', function() {}, element)

@popomore popomore mentioned this issue Apr 2, 2013
15 tasks
@lianqin7
Copy link
Member Author

lianqin7 commented Apr 3, 2013

个人觉得 delegateEvents(element, 'click selector', function() {}) 这种用法更容易和原来的用法区分

@popomore
Copy link
Member

undelegateEvents 也要去除传入 element 上的事件

支持的调用方式

undelegateEvents()
undelegateEvents('click')
undelegateEvents('click li')
undelegateEvents('#trigger')
undelegateEvents('#trigger', 'click')
undelegateEvents('#trigger', 'click li')

popomore added a commit that referenced this issue Apr 15, 2013
```
this.delegateEvents()
this.delegateEvents({
  'click p': 'fn1',
  'click li': function() {}
})
this.delegateEvents('click p', fn1)
this.delegateEvents('click p', function() {})
this.delegateEvents('#trigger', 'click p', fn1)
this.delegateEvents($('#trigger'), 'click', function() {})

```
popomore added a commit that referenced this issue Apr 15, 2013
```
.undelegateEvents()
.undelegateEvents(events)
.undelegateEvents(element, events)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants