customevent.html revision 21f450f4fd5fd60f951de44878aae015ed0342e3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin //YUI.add("selector-native", function(){});
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin filter: "debug",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // combine: false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin useConsole: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin onCSS: function(Y) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('CSS is done loading');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // logInclude: ['event', 'test']
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin logExclude: {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin 'deprecated': 1,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin get: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Dom: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Selector: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Node: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin yui: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin attribute: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin base: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin loader: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin widget: true
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin filters: {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin base: 'raw',
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // dom: null,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin attribute: 'min'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }).use("dump", "test", "console", function(Y) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // Y.Global.on('yui:log', function(e) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var button = Y.get('#btnRun');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // Set up the page
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin button.set("disabled", false);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on("click", function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }, button);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var myConsole = new Y.Console().render();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var testEventTarget = new Y.Test.Case({
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin name: "Custom event tests",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testAugment: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var O = function(id) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('O constructor executed ' + id);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oOo: function(ok) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // pass configuration info into EventTarget with the following
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // construct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin emitFacade: true
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var o = new O(),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin handle = o.on('testAugment', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testAugment', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // if the first argument is not an object, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // event facade is moved in front of the args rather
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // than overwriting existing object.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.on('testAugment', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testAugment', 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testExtend: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var Base = function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('Base constructor executed');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin arguments.callee.superclass.constructor.apply(this, arguments);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin base: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('all your base...');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var b = new Base();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b.on('testExtend', function(arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof Base);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b.fire('testExtend', 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testPrefix: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired1 = false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var O = function(id) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('O constructor executed ' + id);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oOo: function(ok) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // pass configuration info into EventTarget with the following
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // construct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin emitFacade: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin prefix: 'prefix'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var o = new O();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.on('testPrefix', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.on('prefix:testPrefix', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testDetachKey: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired1 = false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // one listener
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.detach('handle|test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // first lisener detached, added a new listener
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.detach('handle|test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.after('handle|test:event', function(arg1) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.areEqual('orange', arg1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // comma or pipe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function(arg1) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.areEqual('orange', arg1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // testing on and after order
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event', 'orange');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // spaces after the comma or lack thereof should have
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // no effect on the addition or removal of listeners
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.detach('handle|test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // added both an on listener and an after listener,
Y.fire('test:event', 'orange');
Y.Assert.isFalse(fired1);
Y.Assert.isFalse(fired2);
Y.after('handle|event2', function() {
Y.on('handle|event2', function() {
Y.detach('handle|*');
Y.fire('event2');
Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
Y.Assert.isTrue(fired2);
Y.Assert.isFalse(fired1);
base:'/build/'
var o = new Y2.EventTarget({
// Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');
o.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');
Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
Y.Assert.isTrue(fired5); // verifies custom prefix
Y.on({
Y.fire('y:click');
Y.fire('y:clack');
Y.Assert.isTrue(f1);
Y.Assert.isTrue(f2);
a = new Y.EventTarget(config),
b = new Y.EventTarget(config);
b.addTarget(a);
b.publish('test:foo');
a.on('test:foo', function() {
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isTrue(ret);
b.on('test:foo', function(e) {
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isFalse(ret);
Y.publish('y:foo1', {
Y.Assert.isTrue(this instanceof YUI);
Y.on('y:foo1', function(e) {
Y.on('y:foo1', function(e) {
Y.fire('y:foo1');
Y.Assert.areEqual(1, count);
Y.publish('y:foo', {
Y.on('y:foo', function(e) {
handle = Y.on('y:foo', function(e) {
handle2 = Y.on('y:foo', function(e) {
Y.detach(handle);
Y.fire('y:foo');
Y.Assert.areEqual(1, count);
var handle3 = Y.on('y:click', function() {
Y.fire('y:click');
Y.fire('y:click');
var o = new Y.EventTarget();
o.on('foo', function(e) {
o.on('foo', function(e) {
o.detachAll();
o.fire('foo');
Y.Assert.areEqual(0, count);
var handle3 = Y.on('y:click', function() {
Y.detachAll();
Y.fire('y:click');
Y.Assert.areEqual(1, count);
var o = new Y.EventTarget(), s1, s2, s3, s4;
o.publish('y:foo2', {
Y.on('y:foo2', function() {
Y.log('Y foo2 executed');
Y.Global.on('y:foo2', function() {
Y.log('GLOBAL foo2 executed');
o.fire('y:foo2');
Y.Assert.areEqual(1, s1);
Y.Assert.areNotEqual(1, s2);
o.publish('y:bar', {
Y.on('y:bar', function() {
Y.log('Y bar executed');
Y.Global.on('y:bar', function() {
Y.log('GLOBAL bar executed');
o.fire('y:bar');
Y.Assert.areEqual(1, s3);
Y.Assert.areEqual(1, s4);
Y.publish('fireonce', {
Y.fire('fireonce', 'foo', 'bar');
Y.on('fireonce', function(arg1, arg2) {
Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener')
Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener')
Y.fire('fireonce', 'foo2', 'bar2');
Y.fire('fireonce', 'foo3', 'bar3');
Y.on('fireonce', function(arg1, arg2) {
Y.log('the notification is asynchronous, so I need to wait for this test');
Y.Assert.areEqual(1, notified, 'listener notified more than once.');
Y.Test.Runner.add(testEventTarget);
// base: "/build/",