zjquery: Show multiple handlers in demo code.

This also makes the error messaging for true
duplicates a bit more specific.
This commit is contained in:
Steve Howell 2018-07-18 11:39:53 +00:00 committed by showell
parent d3baa43167
commit eeac8cfa41
2 changed files with 19 additions and 7 deletions

View File

@ -135,8 +135,13 @@ run_test('events', () => {
var value;
function initialize_handler() {
$('#my-parent').on('input', '.some-child-class', function (e) {
value = 42; // just a dummy side effect
$('#my-parent').on('click', '.button-red', function (e) {
value = 'red'; // just a dummy side effect
e.stopPropagation();
});
$('#my-parent').on('click', '.button-blue', function (e) {
value = 'blue';
e.stopPropagation();
});
}
@ -147,7 +152,7 @@ run_test('events', () => {
// We want to call the inner function, so first let's get it using the
// get_on_handler() helper from zjquery.
var handler_func = $('#my-parent').get_on_handler('input', '.some-child-class');
var red_handler_func = $('#my-parent').get_on_handler('click', '.button-red');
// Set up a stub event so that stopPropagation doesn't explode on us.
var stub_event = {
@ -155,10 +160,15 @@ run_test('events', () => {
};
// Now call the hander.
handler_func(stub_event);
red_handler_func(stub_event);
// And verify it did what it was supposed to do.
assert.equal(value, 42);
assert.equal(value, 'red');
// Test we can have multiple click handlers in the parent.
var blue_handler_func = $('#my-parent').get_on_handler('click', '.button-blue');
blue_handler_func(stub_event);
assert.equal(value, 'blue');
});
run_test('create', () => {

View File

@ -35,11 +35,13 @@ exports.make_event_store = (selector) => {
get_on_handler: function (name, child_selector) {
var funcs = self.get_on_handlers(name, child_selector);
var handler_name = name + ' with ' + child_selector;
if (funcs.length === 0) {
throw Error('Could not find handler for ' + name);
throw Error('Could not find handler for ' + handler_name);
}
if (funcs.length > 1) {
throw Error('Found too many handlers for ' + name);
var remedy = "\nMaybe clear zjquery between tests?";
throw Error('Found too many handlers for ' + handler_name + remedy);
}
return funcs[0];
},