8e267cc5 by Michael Richards

Merge pull request #235 from EloB/optimized-utils

Improved the performance of bindEvent, unbindEvent, getInputValue
2 parents d6049891 05611929
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
13 if $el.attr('type') is 'checkbox' then $el.is ':checked'
14 else do $el.val
15 else
16 Rivets.Util =
17 bindEvent: do ->
18 if 'addEventListener' of window then return (el, event, handler) ->
12 el.addEventListener event, handler, false 19 el.addEventListener event, handler, false
13 else
14 event = 'on' + event
15 el.attachEvent event, handler
16 20
17 # Remove a single DOM event binding. 21 (el, event, handler) -> el.attachEvent 'on' + event, handler
18 unbindEvent: (el, event, handler) -> 22 unbindEvent: do ->
19 if window.jQuery? 23 if 'removeEventListener' of window then return (el, event, handler) ->
20 el = jQuery el
21 if el.off? then el.off event, handler else el.unbind event, handler
22 else if window.removeEventListener?
23 el.removeEventListener event, handler, false 24 el.removeEventListener event, handler, false
24 else
25 event = 'on' + event
26 el.detachEvent event, handler
27 25
28 # Get the current value of an input node. 26 (el, event, handler) -> el.detachEvent 'on' + event, handler
29 getInputValue: (el) -> 27 getInputValue: (el) ->
30 if window.jQuery? 28 if el.type is 'checkbox' then el.checked
31 el = jQuery el 29 else if el.type is 'select-multiple' then o.value for o in el when o.selected
32
33 switch el[0].type
34 when 'checkbox' then el.is ':checked'
35 else el.val()
36 else
37 switch el.type
38 when 'checkbox' then el.checked
39 when 'select-multiple' then o.value for o in el when o.selected
40 else el.value 30 else el.value
...\ No newline at end of file ...\ No newline at end of file
......