customevent.html revision 21f450f4fd5fd60f951de44878aae015ed0342e3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<html>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<head>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<title>YUI3 Custom Event Tests</title>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin</head>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<body class="yui-skin-sam">
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<h1>Event Tests</h1>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin<script type="text/javascript">
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin //YUI.add("selector-native", function(){});
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin YUI({
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin filter: "debug",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // combine: false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin useConsole: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin onCSS: function(Y) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('CSS is done loading');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin },
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 },
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin filters: {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin base: 'raw',
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // dom: null,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin attribute: 'min'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }).use("dump", "test", "console", function(Y) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // Y.Global.on('yui:log', function(e) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // console.log('GLOBAL LOG: ' + e.msg);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var button = Y.get('#btnRun');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // Set up the page
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin button.set("disabled", false);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on("click", function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Test.Runner.run();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }, button);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var myConsole = new Y.Console().render();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('{}');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var testEventTarget = new Y.Test.Case({
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin name: "Custom event tests",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testAugment: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var O = function(id) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin this.id = id;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('O constructor executed ' + id);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin O.prototype = {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oOo: function(ok) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('oOo');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // pass configuration info into EventTarget with the following
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // construct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.augment(O, Y.EventTarget, null, null, {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin emitFacade: true
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var o = new O(),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin handle = o.on('testAugment', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(e instanceof Y.Event.Facade);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(e.foo === 'afoo');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(e.details[1] === 1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(arg1 === 1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(arg2 === 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testAugment', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin handle.detach();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
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 Y.Assert.areEqual(1, arg1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.areEqual(2, arg2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testAugment', 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin },
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testExtend: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var Base = function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('Base constructor executed');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin arguments.callee.superclass.constructor.apply(this, arguments);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.extend(Base, Y.EventTarget, {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin base: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('all your base...');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var b = new Base();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b.on('testExtend', function(arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof Base);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(arg1 === 1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(arg2 === 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin b.fire('testExtend', 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin },
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testPrefix: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired1 = false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var O = function(id) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin this.id = id;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('O constructor executed ' + id);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin O.prototype = {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin oOo: function(ok) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.log('oOo');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // pass configuration info into EventTarget with the following
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // construct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.augment(O, Y.EventTarget, null, null, {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin emitFacade: true,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin prefix: 'prefix'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var o = new O();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.on('testPrefix', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.on('prefix:testPrefix', function(e, arg1, arg2) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(this instanceof O);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // Y.Assert.isTrue(fired2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin },
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin testDetachKey: function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin var fired1 = false,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // one listener
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isFalse(fired2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.detach('handle|test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function() {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // first lisener detached, added a new listener
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isFalse(fired1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.detach('handle|test:event');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.after('handle|test:event', function(arg1) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.areEqual('orange', arg1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isTrue(fired1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // comma or pipe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.on('handle|test:event', function(arg1) {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.areEqual('orange', arg1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.Assert.isFalse(fired2);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = true;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin });
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // testing on and after order
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Y.fire('test:event', 'orange');
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired1 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fired2 = false;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
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
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin // added both an on listener and an after listener,
// then detached both
Y.fire('test:event', 'orange');
Y.Assert.isFalse(fired1);
Y.Assert.isFalse(fired2);
},
testDetachAllByKey: function() {
var fired1 = false,
fired2 = false;
Y.after('handle|event2', function() {
fired2 = true;
});
Y.on('handle|event2', function() {
fired1 = true;
});
// detachAll
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.');
},
testChain: function() {
var fired1 = false,
fired2 = false,
fired3 = false,
fired4 = false,
fired5 = false;
// should be executed once, after f2
var f1 = function() {
Y.Assert.isTrue(fired2);
fired1 = true;
};
// should be executed once, before f1
var f2 = function() {
Y.Assert.isFalse(fired1);
fired2 = true;
};
// should be executed once, different event from f1 and f2
var f3 = function() {
fired3 = true;
};
// detached before fired, should not executed
var f4 = function() {
fired4 = true;
};
// should fire once, preserving the custom prefix rather
// than using the configured event target prefix
var f5 = function() {
fired5 = true;
};
// configure chaining via global default or on the event target
YUI({ /* chain: true */
base:'/build/'
}).use('*', function(Y2) {
var o = new Y2.EventTarget({
prefix: 'foo',
chain : true
});
// without event target prefix manipulation (incomplete now)
// @TODO an error here is throwing an uncaught exception rather than failing the test
// 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');
// with event target prefix manipulation ('e' is the same event as 'foo:e',
// but 'pre:e' is a different event only accessible by using that exact name)
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(fired1); // verifies chaining, on/after order, and adding the event target prefix
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
});
},
testObjType: function() {
var f1, f2;
Y.on({
'y:click': function() {f1 = true},
'y:clack': function() {f2 = true}
});
Y.fire('y:click');
Y.fire('y:clack');
Y.Assert.isTrue(f1);
Y.Assert.isTrue(f2);
},
testBubble: function() {
var count = 0,
ret,
config = {
emitFacade: true,
bubbles: true
},
a = new Y.EventTarget(config),
b = new Y.EventTarget(config);
b.addTarget(a);
// this should not be necessary
b.publish('test:foo');
a.on('test:foo', function() {
count++;
});
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isTrue(ret);
b.on('test:foo', function(e) {
e.stopPropagation();
});
ret = b.fire('test:foo', {}, b);
Y.Assert.areEqual(1, count);
Y.Assert.isFalse(ret);
},
testPreventFnOnce: function() {
var count = 0;
Y.publish('y:foo1', {
emitFacade: true,
preventedFn: function() {
count++;
Y.Assert.isTrue(this instanceof YUI);
}
});
Y.on('y:foo1', function(e) {
e.preventDefault();
});
Y.on('y:foo1', function(e) {
e.preventDefault();
});
Y.fire('y:foo1');
Y.Assert.areEqual(1, count);
},
testDetachHandle: function() {
var count = 0, handle, handle2;
Y.publish('y:foo', {
emitFacade: true
});
Y.on('y:foo', function(e) {
count++;
handle2.detach();
});
handle = Y.on('y:foo', function(e) {
count += 100;
});
handle2 = Y.on('y:foo', function(e) {
count += 1000;
});
Y.detach(handle);
Y.fire('y:foo');
Y.Assert.areEqual(1, count);
count = 0;
var handle3 = Y.on('y:click', function() {
count++;
handle3.detach();
});
Y.fire('y:click');
Y.fire('y:click');
var o = new Y.EventTarget();
count = 0;
o.on('foo', function(e) {
count++;
});
o.on('foo', function(e) {
count++;
});
o.detachAll();
o.fire('foo');
Y.Assert.areEqual(0, count);
var handle3 = Y.on('y:click', function() {
count++;
});
// detachAll can't be allowed to work on the YUI instance.
Y.detachAll();
Y.fire('y:click');
Y.Assert.areEqual(1, count);
},
testBroadcast: function() {
var o = new Y.EventTarget(), s1, s2, s3, s4;
o.publish('y:foo2', {
emitFacade: true,
broadcast: 1
});
Y.on('y:foo2', function() {
Y.log('Y foo2 executed');
s1 = 1;
});
Y.Global.on('y:foo2', function() {
Y.log('GLOBAL foo2 executed');
s2 = 1;
});
o.fire('y:foo2');
Y.Assert.areEqual(1, s1);
Y.Assert.areNotEqual(1, s2);
s1 = 0;
s2 = 0;
o.publish('y:bar', {
emitFacade: true,
broadcast: 2
});
Y.on('y:bar', function() {
Y.log('Y bar executed');
s3 = 1;
});
Y.Global.on('y:bar', function() {
Y.log('GLOBAL bar executed');
s4 = 1;
});
o.fire('y:bar');
Y.Assert.areEqual(1, s3);
Y.Assert.areEqual(1, s4);
},
test_fire_once: function() {
var notified = 0;
Y.publish('fireonce', {
fireOnce: true
});
Y.fire('fireonce', 'foo', 'bar');
Y.on('fireonce', function(arg1, arg2) {
notified++;
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);
Y.Test.Runner.run();
});
// YUI({
// base: "/build/",
// filter: "debug",
// combine: false,
// useConsole: true,
// logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, loader: true, get: true, widget: true}
// }).use("datasource", function(Y) {
// Y.log('loaded datasource: ' + Y.DataSource);
// });
})();
</script>
</body>
</html>