a5e3414f by Michael Richards

Merge pull request #170 from mikeric/event-handler-configuration

Event Handler Configuration
2 parents c22a01d8 bbc15037
...@@ -251,30 +251,26 @@ class Rivets.View ...@@ -251,30 +251,26 @@ class Rivets.View
251 # Houses common utility functions used internally by Rivets.js. 251 # Houses common utility functions used internally by Rivets.js.
252 Rivets.Util = 252 Rivets.Util =
253 # Create a single DOM event binding. 253 # Create a single DOM event binding.
254 bindEvent: (el, event, handler, view) -> 254 bindEvent: (el, event, handler) ->
255 fn = (ev) -> handler.call @, ev, view
256
257 if window.jQuery? 255 if window.jQuery?
258 el = jQuery el 256 el = jQuery el
259 if el.on? then el.on event, fn else el.bind event, fn 257 if el.on? then el.on event, handler else el.bind event, handler
260 else if window.addEventListener? 258 else if window.addEventListener?
261 el.addEventListener event, fn, false 259 el.addEventListener event, handler, false
262 else 260 else
263 event = 'on' + event 261 event = 'on' + event
264 el.attachEvent event, fn 262 el.attachEvent event, handler
265
266 fn
267 263
268 # Remove a single DOM event binding. 264 # Remove a single DOM event binding.
269 unbindEvent: (el, event, fn) -> 265 unbindEvent: (el, event, handler) ->
270 if window.jQuery? 266 if window.jQuery?
271 el = jQuery el 267 el = jQuery el
272 if el.off? then el.off event, fn else el.unbind event, fn 268 if el.off? then el.off event, handler else el.unbind event, handler
273 else if window.removeEventListener 269 else if window.removeEventListener?
274 el.removeEventListener event, fn, false 270 el.removeEventListener event, handler, false
275 else 271 else
276 event = 'on' + event 272 event = 'on' + event
277 el.detachEvent event, fn 273 el.detachEvent event, handler
278 274
279 # Get the current value of an input node. 275 # Get the current value of an input node.
280 getInputValue: (el) -> 276 getInputValue: (el) ->
...@@ -306,9 +302,9 @@ Rivets.binders = ...@@ -306,9 +302,9 @@ Rivets.binders =
306 checked: 302 checked:
307 publishes: true 303 publishes: true
308 bind: (el) -> 304 bind: (el) ->
309 @currentListener = Rivets.Util.bindEvent el, 'change', @publish 305 Rivets.Util.bindEvent el, 'change', @publish
310 unbind: (el) -> 306 unbind: (el) ->
311 Rivets.Util.unbindEvent el, 'change', @currentListener 307 Rivets.Util.unbindEvent el, 'change', @publish
312 routine: (el, value) -> 308 routine: (el, value) ->
313 if el.type is 'radio' 309 if el.type is 'radio'
314 el.checked = el.value?.toString() is value?.toString() 310 el.checked = el.value?.toString() is value?.toString()
...@@ -318,9 +314,9 @@ Rivets.binders = ...@@ -318,9 +314,9 @@ Rivets.binders =
318 unchecked: 314 unchecked:
319 publishes: true 315 publishes: true
320 bind: (el) -> 316 bind: (el) ->
321 @currentListener = Rivets.Util.bindEvent el, 'change', @publish 317 Rivets.Util.bindEvent el, 'change', @publish
322 unbind: (el) -> 318 unbind: (el) ->
323 Rivets.Util.unbindEvent el, 'change', @currentListener 319 Rivets.Util.unbindEvent el, 'change', @publish
324 routine: (el, value) -> 320 routine: (el, value) ->
325 if el.type is 'radio' 321 if el.type is 'radio'
326 el.checked = el.value?.toString() isnt value?.toString() 322 el.checked = el.value?.toString() isnt value?.toString()
...@@ -339,9 +335,9 @@ Rivets.binders = ...@@ -339,9 +335,9 @@ Rivets.binders =
339 value: 335 value:
340 publishes: true 336 publishes: true
341 bind: (el) -> 337 bind: (el) ->
342 @currentListener = Rivets.Util.bindEvent el, 'change', @publish 338 Rivets.Util.bindEvent el, 'change', @publish
343 unbind: (el) -> 339 unbind: (el) ->
344 Rivets.Util.unbindEvent el, 'change', @currentListener 340 Rivets.Util.unbindEvent el, 'change', @publish
345 routine: (el, value) -> 341 routine: (el, value) ->
346 if window.jQuery? 342 if window.jQuery?
347 el = jQuery el 343 el = jQuery el
...@@ -362,9 +358,15 @@ Rivets.binders = ...@@ -362,9 +358,15 @@ Rivets.binders =
362 358
363 "on-*": 359 "on-*":
364 function: true 360 function: true
361
362 unbind: (el) ->
363 Rivets.Util.unbindEvent el, @args[0], @handler if @handler
364
365 routine: (el, value) -> 365 routine: (el, value) ->
366 Rivets.Util.unbindEvent el, @args[0], @currentListener if @currentListener 366 binding = this
367 @currentListener = Rivets.Util.bindEvent el, @args[0], value, @view 367 Rivets.Util.unbindEvent el, @args[0], @handler if @handler
368 @handler = (ev) -> binding.view.config.handler value, @, ev, binding
369 Rivets.Util.bindEvent el, @args[0], @handler
368 370
369 "each-*": 371 "each-*":
370 block: true 372 block: true
...@@ -444,6 +446,8 @@ Rivets.binders = ...@@ -444,6 +446,8 @@ Rivets.binders =
444 # overridden globally or local to a `Rivets.View` instance. 446 # overridden globally or local to a `Rivets.View` instance.
445 Rivets.config = 447 Rivets.config =
446 preloadData: true 448 preloadData: true
449 handler: (fn, context, ev, binding) ->
450 fn.call context, ev, binding.view.models
447 451
448 # Rivets.formatters 452 # Rivets.formatters
449 # ----------------- 453 # -----------------
......