05611929 by Olle Bröms

Improved the performance of bindEvent, unbindEvent, getInputValue on Rivets.Util.

1 parent d6049891
1 # Rivets.Util 1 # Rivets.Util
2 # ----------- 2 # -----------
3 3
4 # Houses common utility functions used internally by Rivets.js. 4 if 'jQuery' of window
5 Rivets.Util = 5 [bindMethod, unbindMethod] = if 'on' of jQuery then ['on', 'off'] else ['bind', 'unbind']
6 # Create a single DOM event binding. 6
7 bindEvent: (el, event, handler) -> 7 Rivets.Util =
8 if window.jQuery? 8 bindEvent: (el, event, handler) -> jQuery(el)[bindMethod] event, handler
9 el = jQuery el 9 unbindEvent: (el, event, handler) -> jQuery(el)[unbindMethod] event, handler
10 if el.on? then el.on event, handler else el.bind event, handler 10 getInputValue: (el) ->
11 else if window.addEventListener? 11 $el = jQuery el
12 el.addEventListener event, handler, false
13 else
14 event = 'on' + event
15 el.attachEvent event, handler
16 12
17 # Remove a single DOM event binding. 13 if $el.attr('type') is 'checkbox' then $el.is ':checked'
18 unbindEvent: (el, event, handler) -> 14 else do $el.val
19 if window.jQuery? 15 else
20 el = jQuery el 16 Rivets.Util =
21 if el.off? then el.off event, handler else el.unbind event, handler 17 bindEvent: do ->
22 else if window.removeEventListener? 18 if 'addEventListener' of window then return (el, event, handler) ->
23 el.removeEventListener event, handler, false 19 el.addEventListener event, handler, false
24 else
25 event = 'on' + event
26 el.detachEvent event, handler
27 20
28 # Get the current value of an input node. 21 (el, event, handler) -> el.attachEvent 'on' + event, handler
29 getInputValue: (el) -> 22 unbindEvent: do ->
30 if window.jQuery? 23 if 'removeEventListener' of window then return (el, event, handler) ->
31 el = jQuery el 24 el.removeEventListener event, handler, false
32 25
33 switch el[0].type 26 (el, event, handler) -> el.detachEvent 'on' + event, handler
34 when 'checkbox' then el.is ':checked' 27 getInputValue: (el) ->
35 else el.val() 28 if el.type is 'checkbox' then el.checked
36 else 29 else if el.type is 'select-multiple' then o.value for o in el when o.selected
37 switch el.type 30 else el.value
38 when 'checkbox' then el.checked
39 when 'select-multiple' then o.value for o in el when o.selected
40 else el.value
...\ No newline at end of file ...\ No newline at end of file
......