mirror of https://github.com/zulip/zulip.git
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:
parent
d3baa43167
commit
eeac8cfa41
|
@ -135,8 +135,13 @@ run_test('events', () => {
|
||||||
var value;
|
var value;
|
||||||
|
|
||||||
function initialize_handler() {
|
function initialize_handler() {
|
||||||
$('#my-parent').on('input', '.some-child-class', function (e) {
|
$('#my-parent').on('click', '.button-red', function (e) {
|
||||||
value = 42; // just a dummy side effect
|
value = 'red'; // just a dummy side effect
|
||||||
|
e.stopPropagation();
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#my-parent').on('click', '.button-blue', function (e) {
|
||||||
|
value = 'blue';
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -147,7 +152,7 @@ run_test('events', () => {
|
||||||
|
|
||||||
// We want to call the inner function, so first let's get it using the
|
// We want to call the inner function, so first let's get it using the
|
||||||
// get_on_handler() helper from zjquery.
|
// 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.
|
// Set up a stub event so that stopPropagation doesn't explode on us.
|
||||||
var stub_event = {
|
var stub_event = {
|
||||||
|
@ -155,10 +160,15 @@ run_test('events', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Now call the hander.
|
// Now call the hander.
|
||||||
handler_func(stub_event);
|
red_handler_func(stub_event);
|
||||||
|
|
||||||
// And verify it did what it was supposed to do.
|
// 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', () => {
|
run_test('create', () => {
|
||||||
|
|
|
@ -35,11 +35,13 @@ exports.make_event_store = (selector) => {
|
||||||
|
|
||||||
get_on_handler: function (name, child_selector) {
|
get_on_handler: function (name, child_selector) {
|
||||||
var funcs = self.get_on_handlers(name, child_selector);
|
var funcs = self.get_on_handlers(name, child_selector);
|
||||||
|
var handler_name = name + ' with ' + child_selector;
|
||||||
if (funcs.length === 0) {
|
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) {
|
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];
|
return funcs[0];
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue