86cf7283 by Michael Richards

Pass the binding's model as the function context for event bindings. Also ensure…

… that the wrapped function is passed back to the routine so that we can unbind the old function and rebind the new one when it changes (for when using adapter.subscribe). [Closes #71]
1 parent 0d59fe30
...@@ -51,8 +51,7 @@ class Rivets.Binding ...@@ -51,8 +51,7 @@ class Rivets.Binding
51 @formattedValue value 51 @formattedValue value
52 52
53 if @options.special is 'event' 53 if @options.special is 'event'
54 @routine @el, value, @currentListener 54 @currentListener = @routine @el, @model, value, @currentListener
55 @currentListener = value
56 else if @options.special is 'iteration' 55 else if @options.special is 'iteration'
57 @routine @el, value, @ 56 @routine @el, value, @
58 else 57 else
...@@ -206,7 +205,9 @@ class Rivets.View ...@@ -206,7 +205,9 @@ class Rivets.View
206 binding.publish() for binding in @select (b) -> b.isBidirectional() 205 binding.publish() for binding in @select (b) -> b.isBidirectional()
207 206
208 # Cross-browser event binding. 207 # Cross-browser event binding.
209 bindEvent = (el, event, fn) -> 208 bindEvent = (el, event, handler, context) ->
209 fn = (e) -> handler.call context, e
210
210 # Check to see if jQuery is loaded. 211 # Check to see if jQuery is loaded.
211 if window.jQuery? 212 if window.jQuery?
212 el = jQuery el 213 el = jQuery el
...@@ -219,6 +220,8 @@ bindEvent = (el, event, fn) -> ...@@ -219,6 +220,8 @@ bindEvent = (el, event, fn) ->
219 event = 'on' + event 220 event = 'on' + event
220 el.attachEvent event, fn 221 el.attachEvent event, fn
221 222
223 fn
224
222 # Cross-browser event unbinding. 225 # Cross-browser event unbinding.
223 unbindEvent = (el, event, fn) -> 226 unbindEvent = (el, event, fn) ->
224 # Check to see if jQuery is loaded. 227 # Check to see if jQuery is loaded.
...@@ -240,9 +243,9 @@ getInputValue = (el) -> ...@@ -240,9 +243,9 @@ getInputValue = (el) ->
240 else el.value 243 else el.value
241 244
242 # Returns an event binding routine for the specified event. 245 # Returns an event binding routine for the specified event.
243 eventBinding = (event) -> (el, bind, unbind) -> 246 eventBinding = (event) -> (el, context, bind, unbind) ->
244 bindEvent el, event, bind if bind
245 unbindEvent el, event, unbind if unbind 247 unbindEvent el, event, unbind if unbind
248 bindEvent el, event, bind, context
246 249
247 # Returns a class binding routine for the specified class name. 250 # Returns a class binding routine for the specified class name.
248 classBinding = (name) -> (el, value) -> 251 classBinding = (name) -> (el, value) ->
......