From 8f0daf562ea75e67248b99dff8f3f26199414b9d Mon Sep 17 00:00:00 2001 From: Ryan Van Etten Date: Sun, 16 Feb 2014 08:58:02 -0500 Subject: [PATCH 1/4] Add fire-demo.html to inspect handler arguments (#80, #102) --- tests/fire-demo.html | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/fire-demo.html diff --git a/tests/fire-demo.html b/tests/fire-demo.html new file mode 100644 index 0000000..f4142e5 --- /dev/null +++ b/tests/fire-demo.html @@ -0,0 +1,33 @@ + +bean.fire() demo + + +

Open the console.

+
div#test
+ + + \ No newline at end of file From ea02b5999bf6b371b53e83e93fe1e44197b95c08 Mon Sep 17 00:00:00 2001 From: Ryan Van Etten Date: Sun, 16 Feb 2014 09:00:16 -0500 Subject: [PATCH 2/4] Normalize handler signature per #102 and #80 --- src/bean.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/bean.js b/src/bean.js index 20b97c3..0a84225 100644 --- a/src/bean.js +++ b/src/bean.js @@ -246,14 +246,14 @@ // each handler is wrapped so we can handle delegation and custom events var wrappedHandler = function (element, fn, condition, args) { var call = function (event, eargs) { - return fn.apply(element, args ? slice.call(eargs, event ? 0 : 1).concat(args) : eargs) + return fn.apply(element, args ? slice.call(eargs).concat(args) : eargs) } , findTarget = function (event, eventElement) { return fn.__beanDel ? fn.__beanDel.ft(event.target, element) : eventElement } , handler = condition ? function (event) { - var target = findTarget(event, this) // deleated event + var target = findTarget(event, this) // delegated event if (condition.apply(target, arguments)) { if (event) event.currentTarget = target return call(event, arguments) @@ -656,7 +656,7 @@ */ , fire = function (element, type, args) { var types = str2arr(type) - , i, j, l, names, handlers + , i, j, l, call, event, names, handlers for (i = types.length; i--;) { type = types[i].replace(nameRegex, '') @@ -667,10 +667,13 @@ // non-native event, either because of a namespace, arguments or a non DOM element // iterate over all listeners and manually 'fire' handlers = registry.get(element, type, null, false) - args = [false].concat(args) + event = new Event(null, element, nativeEvents[type]) + event.type = type + call = args ? 'apply' : 'call' + args = args ? [event].concat(args) : event for (j = 0, l = handlers.length; j < l; j++) { if (handlers[j].inNamespaces(names)) { - handlers[j].handler.apply(element, args) + handlers[j].handler[call](element, args) } } } From 5965c57ebb1cd3e236799dac61bf6527f7b012d5 Mon Sep 17 00:00:00 2001 From: Ryan Van Etten Date: Sun, 16 Feb 2014 10:25:20 -0500 Subject: [PATCH 3/4] Rework .fire() tests based on normalized handler signature --- tests/fire-test.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/fire-test.js b/tests/fire-test.js index d2d240a..6087a07 100644 --- a/tests/fire-test.js +++ b/tests/fire-test.js @@ -35,22 +35,25 @@ buster.testCase('fire', { bean.fire(el, 'mousedown mouseup') } - , 'should be able to pass multiple arguments to custom event': function (done) { + , 'should be able to pass extra arguments to custom event': function (done) { // jquery like array syntax var el = this.byId('input') , trigger = this.trigger() , spy = this.spy() + , extra = [1, 2, 3] + , slice = extra.slice + , type = 'foo' trigger.after(function () { assert.equals(spy.callCount, 1, 'single call') - assert.equals(spy.firstCall.args.length, 3, 'called with 3 arguments') - assert.equals(spy.firstCall.args[0], 1, 'called with correct argument 1') - assert.equals(spy.firstCall.args[1], 2, 'called with correct argument 2') - assert.equals(spy.firstCall.args[2], 3, 'called with correct argument 3') + assert.equals(spy.firstCall.args.length, 1+extra.length, 'called with correct arguments.length') + assert.equals(typeof(spy.firstCall.args[0] || 0), 'object', 'called with correct arguments[0] type') + assert.equals(spy.firstCall.args[0].type, type.split('.')[0], 'called with correct event.type') + assert.equals(slice.call(spy.firstCall.args, 1).join(), extra.join(), 'called with correct extra arguments') done() }) - bean.on(el, 'foo', trigger.wrap(spy)) - bean.fire(el, 'foo', [1, 2, 3]) + bean.on(el, type, trigger.wrap(spy)) + bean.fire(el, type, extra) } }) \ No newline at end of file From ec3c078be90617e0e9d092130888a23ca5cc474f Mon Sep 17 00:00:00 2001 From: Ryan Van Etten Date: Mon, 24 Feb 2014 01:04:40 -0500 Subject: [PATCH 4/4] Make demo more exhaustive --- tests/fire-demo.html | 57 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/tests/fire-demo.html b/tests/fire-demo.html index f4142e5..4f577f7 100644 --- a/tests/fire-demo.html +++ b/tests/fire-demo.html @@ -13,21 +13,52 @@ \ No newline at end of file