Skip to content

Commit

Permalink
Accept function as data for on event handler
Browse files Browse the repository at this point in the history
Move the parameter data to callback, if callback is undefined or data is
false. As the data can hold any type, including function, the check is
done on the callback, to see if it is undefined, before moving the
parameters.

Fixes madrobby#1042
  • Loading branch information
carbon-tigerlab committed Jan 2, 2015
1 parent 28896e3 commit 508d190
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@

if (!isString(selector) && !isFunction(callback) && callback !== false)
callback = data, data = selector, selector = undefined
if (isFunction(data) || data === false)
if (callback === undefined || data === false)
callback = data, data = undefined

if (callback === false) callback = returnFalse
Expand Down
15 changes: 15 additions & 0 deletions test/event.html
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@ <h1>Zepto Event unit tests</h1>
t.assertEqual(1, numArgs)
t.assertIdentical(data, gotData)
},

testHandlerWithFunctionAsData: function(t){
var data = function () { console.log('foo') },
gotData, numArgs

$(document).on('click', data, function(event){
gotData = event.data
numArgs = arguments.length
})
t.assertUndefined(gotData)

click(this.el)
t.assertEqual(1, numArgs)
t.assertIdentical(data, gotData)
},

testDelegatedWithData: function(t){
var data = {}, gotData, numArgs
Expand Down

0 comments on commit 508d190

Please sign in to comment.