customevent.html revision 0dca577a07715960da42d47787eecc25b285182f
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<html>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<head>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<title>YUI3 Custom Event Tests</title>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith</head>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<body class="yui-skin-sam">
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<h1>Event Tests</h1>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<div id="adiv">a div</div>
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith<script type="text/javascript">
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith(function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith //YUI.add("selector-native", function(){});
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith YUI({
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith filter: "debug",
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // combine: false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith useConsole: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith onCSS: function(Y) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.log('CSS is done loading', 'info', 'testcase');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // logInclude: ['event', 'test']
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith logExclude: {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith 'deprecated': 1,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith get: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Dom: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Selector: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Node: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith yui: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith attribute: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith event: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith deprecated: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith base: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith loader: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith widget: true
5de67636ab6229eb65d875a4e2f6f19aa41bc1a6John Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
5a5d529d0cac2d73b19776530d8a344024f4abccMartin Owens filters: {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith base: 'raw',
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // dom: null,
5918336f232770abf2da7efea2e1b35096105034John Smith attribute: 'min'
5918336f232770abf2da7efea2e1b35096105034John Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }).use("dump", "test", "console", function(Y) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
48ccbd047a46be9ea741214e50ce912ea2c5c5beJohn Smith // Y.Global.on('yui:log', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // console.log('GLOBAL LOG: ' + e.msg);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // });
48ccbd047a46be9ea741214e50ce912ea2c5c5beJohn Smith
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith var button = Y.get('#btnRun');
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // Set up the page
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith button.set("disabled", false);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on("click", function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Test.Runner.run();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }, button);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var myConsole = new Y.Console().render();
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens Y.log('{}');
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens var testEventTarget = new Y.Test.Case({
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens name: "Custom event tests",
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens testAugment: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var fired = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var O = function(id) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith this.id = id;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.log('O constructor executed ' + id);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith O.prototype = {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith oOo: function(ok) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.log('oOo');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // pass configuration info into EventTarget with the following
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // construct
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.augment(O, Y.EventTarget, null, null, {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith emitFacade: true
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var o = new O(),
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith handle = o.on('testAugment', function(e, arg1, arg2) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(this instanceof O);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(e instanceof Y.Event.Facade);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(e.foo === 'afoo');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(e.details[1] === 1);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(arg1 === 1);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(arg2 === 2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith fired = true;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith o.fire('testAugment', { foo: 'afoo' }, 1, 2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(fired);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith handle.detach();
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // if the first argument is not an object, the
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // event facade is moved in front of the args rather
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // than overwriting existing object.
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith o.on('testAugment', function(e, arg1, arg2) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.areEqual(1, arg1);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.areEqual(2, arg2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith o.fire('testAugment', 1, 2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith },
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
b3aad357684d3a0ef78dca903863a44a0aab9d14John Smith testExtend: function() {
b3aad357684d3a0ef78dca903863a44a0aab9d14John Smith
b3aad357684d3a0ef78dca903863a44a0aab9d14John Smith var fired = false;
b3aad357684d3a0ef78dca903863a44a0aab9d14John Smith
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith var Base = function() {
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.log('Base constructor executed');
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith arguments.callee.superclass.constructor.apply(this, arguments);
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith }
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.extend(Base, Y.EventTarget, {
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith base: function() {
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.log('all your base...');
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith }
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith });
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith var b = new Base();
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith b.on('testExtend', function(arg1, arg2) {
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.Assert.isTrue(this instanceof Base);
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.Assert.isTrue(arg1 === 1);
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith Y.Assert.isTrue(arg2 === 2);
47b9f5ea8422d7f057fe469f3cf45c559c5e09cbJohn Smith fired = true;
b3aad357684d3a0ef78dca903863a44a0aab9d14John Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith b.fire('testExtend', 1, 2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testPrefix: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var fired1 = false,
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith fired2 = false;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var O = function(id) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith this.id = id;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.log('O constructor executed ' + id);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith O.prototype = {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith oOo: function(ok) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.log('oOo');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith }
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens }
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // pass configuration info into EventTarget with the following
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // construct
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.augment(O, Y.EventTarget, null, null, {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith emitFacade: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith prefix: 'prefix'
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var o = new O();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.on('testPrefix', function(e, arg1, arg2) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(this instanceof O);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith fired1 = true;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith o.on('prefix:testPrefix', function(e, arg1, arg2) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(this instanceof O);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith fired2 = true;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // Y.Assert.isTrue(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testDetachKey: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var fired1 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('handle|test:event', function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // one listener
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('test:event');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isFalse(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.detach('handle|test:event');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('handle|test:event', function() {
5918336f232770abf2da7efea2e1b35096105034John Smith fired2 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // first lisener detached, added a new listener
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('test:event');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isFalse(fired1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.detach('handle|test:event');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.after('handle|test:event', function(arg1) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('orange', arg1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // comma or pipe
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('handle|test:event', function(arg1) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('orange', arg1);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isFalse(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // testing on and after order
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris Y.fire('test:event', 'orange');
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris fired1 = false;
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris fired2 = false;
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris // spaces after the comma or lack thereof should have
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris // no effect on the addition or removal of listeners
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris Y.detach('handle|test:event');
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris // added both an on listener and an after listener,
5918336f232770abf2da7efea2e1b35096105034John Smith // then detached both
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris Y.fire('test:event', 'orange');
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris Y.Assert.isFalse(fired1);
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris Y.Assert.isFalse(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testDetachAllByKey: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var fired1 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.after('handle|event2', function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = true;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.on('handle|event2', function() {
5918336f232770abf2da7efea2e1b35096105034John Smith fired1 = true;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith // detachAll
5918336f232770abf2da7efea2e1b35096105034John Smith Y.detach('handle|*');
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.fire('event2');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testChain: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var fired1 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired2 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired3 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired4 = false,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired5 = false;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris // should be executed once, after f2
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var f1 = function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired2);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired1 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith };
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith // should be executed once, before f1
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var f2 = function() {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isFalse(fired1);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith fired2 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith };
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // should be executed once, different event from f1 and f2
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var f3 = function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired3 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith };
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // detached before fired, should not executed
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var f4 = function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired4 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith };
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // should fire once, preserving the custom prefix rather
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // than using the configured event target prefix
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var f5 = function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fired5 = true;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith };
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // configure chaining via global default or on the event target
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith YUI({ /* chain: true */
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith base:'/build/',
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith logInclude: {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith test: true
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }).use('*', function(Y2) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var o = new Y2.EventTarget({
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith prefix: 'foo',
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith chain : true
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // without event target prefix manipulation (incomplete now)
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // @TODO an error here is throwing an uncaught exception rather than failing the test
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // 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');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // with event target prefix manipulation ('e' is the same event as 'foo:e',
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // but 'pre:e' is a different event only accessible by using that exact name)
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smitho.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');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(fired5); // verifies custom prefix
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testObjType: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var f1, f2;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on({
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith 'y:click': function() {f1 = true},
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith 'y:clack': function() {f2 = true}
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.fire('y:click');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.fire('y:clack');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(f1);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(f2);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith },
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith testBubble: function() {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var count = 0,
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith ret,
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith config = {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith emitFacade: true,
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith bubbles: true
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith },
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith a = new Y.EventTarget(config),
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith b = new Y.EventTarget(config);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith b.addTarget(a);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // this should not be necessary
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith b.publish('test:foo');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith a.on('test:foo', function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith ret = b.fire('test:foo', {}, b);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.areEqual(1, count);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isTrue(ret);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith b.on('test:foo', function(e) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith e.stopPropagation();
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith ret = b.fire('test:foo', {}, b);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.areEqual(1, count);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.isFalse(ret);
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith },
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
dafb7c49e61c99b402626104f8f657939602bbfdMartin Owens testPreventFnOnce: function() {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var count = 0;
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.publish('y:foo1', {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith emitFacade: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith preventedFn: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(this instanceof YUI);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }
5918336f232770abf2da7efea2e1b35096105034John Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('y:foo1', function(e) {
5918336f232770abf2da7efea2e1b35096105034John Smith e.preventDefault();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.on('y:foo1', function(e) {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith e.preventDefault();
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.fire('y:foo1');
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith Y.Assert.areEqual(1, count);
5de67636ab6229eb65d875a4e2f6f19aa41bc1a6John Smith },
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith testDetachHandle: function() {
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith var count = 0, handle, handle2;
5918336f232770abf2da7efea2e1b35096105034John Smith Y.publish('y:foo', {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith emitFacade: true
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith });
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('y:foo', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith handle2.detach();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith handle = Y.on('y:foo', function(e) {
5918336f232770abf2da7efea2e1b35096105034John Smith count += 100;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith handle2 = Y.on('y:foo', function(e) {
5918336f232770abf2da7efea2e1b35096105034John Smith count += 1000;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.detach(handle);
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.fire('y:foo');
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.Assert.areEqual(1, count);
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith count = 0;
5918336f232770abf2da7efea2e1b35096105034John Smith
2647750e10dcc80876a80b0ae09e50466e6caddaJohn Smith var handle3 = Y.on('y:click', function() {
2647750e10dcc80876a80b0ae09e50466e6caddaJohn Smith count++;
2647750e10dcc80876a80b0ae09e50466e6caddaJohn Smith handle3.detach();
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.fire('y:click');
5918336f232770abf2da7efea2e1b35096105034John Smith Y.fire('y:click');
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith var o = new Y.EventTarget();
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith count = 0;
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith o.on('foo', function(e) {
5918336f232770abf2da7efea2e1b35096105034John Smith count++;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.on('foo', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.detachAll();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.fire('foo');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(0, count);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var handle3 = Y.on('y:click', function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // detachAll can't be allowed to work on the YUI instance.
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.detachAll();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('y:click');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(1, count);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testBroadcast: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var o = new Y.EventTarget(), s1, s2, s3, s4;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith o.publish('y:foo2', {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith emitFacade: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith broadcast: 1
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('y:foo2', function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.log('Y foo2 executed');
5918336f232770abf2da7efea2e1b35096105034John Smith s1 = 1;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.Global.on('y:foo2', function() {
5918336f232770abf2da7efea2e1b35096105034John Smith Y.log('GLOBAL foo2 executed');
5918336f232770abf2da7efea2e1b35096105034John Smith s2 = 1;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith o.fire('y:foo2');
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.Assert.areEqual(1, s1);
5918336f232770abf2da7efea2e1b35096105034John Smith Y.Assert.areNotEqual(1, s2);
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith s1 = 0;
5918336f232770abf2da7efea2e1b35096105034John Smith s2 = 0;
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith o.publish('y:bar', {
5918336f232770abf2da7efea2e1b35096105034John Smith emitFacade: true,
5918336f232770abf2da7efea2e1b35096105034John Smith broadcast: 2
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.on('y:bar', function() {
5918336f232770abf2da7efea2e1b35096105034John Smith Y.log('Y bar executed');
5918336f232770abf2da7efea2e1b35096105034John Smith s3 = 1;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith Y.Global.on('y:bar', function() {
5918336f232770abf2da7efea2e1b35096105034John Smith Y.log('GLOBAL bar executed');
5918336f232770abf2da7efea2e1b35096105034John Smith s4 = 1;
5918336f232770abf2da7efea2e1b35096105034John Smith });
5918336f232770abf2da7efea2e1b35096105034John Smith
5918336f232770abf2da7efea2e1b35096105034John Smith o.fire('y:bar');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(1, s3);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(1, s4);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith test_fire_once: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var notified = 0;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.publish('fireonce', {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fireOnce: true
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('fireonce', 'foo', 'bar');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('fireonce', function(arg1, arg2) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith notified++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener')
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener')
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('fireonce', 'foo2', 'bar2');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.fire('fireonce', 'foo3', 'bar3');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.on('fireonce', function(arg1, arg2) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.log('the notification is asynchronous, so I need to wait for this test');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(1, notified, 'listener notified more than once.');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
f628c624dd74535a75a1eae5a1cee460ac02d5c4Kris },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith test_node_publish: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var node = Y.one('#adiv');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var preventCount = 0, heard = 0;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith node.publish('foo1', {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith emitFacade: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // should only be called once
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith preventedFn: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith preventCount++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isTrue(this instanceof Y.Node);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith }
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith node.on('foo1', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('faking foo', e.type);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual('foo1', e._type);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith heard++;
0ce918f80fe50c422e3cb9918b5fb0c3b4613ab7Jon A. Cruz e.preventDefault();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
afe6139d42ccfc3ff3b5358564caf6f2c9f41216John Smith node.on('foo1', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith heard++;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith e.preventDefault();
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith node.fire('foo1', {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith type: 'faking foo'
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith });
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(1, preventCount);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(2, heard);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // SRC, ON
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // BUBBLE, ON
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // BUBBLE, DEFAULT BEHAVIOR
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // BUBBLE, AFTER
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // SRC, DEFAULT BEHAVIOR
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith // SRC, AFTER
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith testBubbleSequence300GA: function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith var count = 0,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith called = null,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith fn = function() {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith called = this.name;
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith config = {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith emitFacade: true,
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith bubbles: true
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith },
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith leaf = new Y.EventTarget(config),
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith branch = new Y.EventTarget(config),
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith root = new Y.EventTarget(config);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith leaf.name = 'leaf';
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith branch.name = 'branch';
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith root.name = 'root';
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith leaf.addTarget(branch);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith branch.addTarget(root);
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith leaf.publish('test:foo', { defaultFn: fn});
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith branch.publish('test:foo', { defaultFn: fn});
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith root.publish('test:foo', { defaultFn: fn});
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith leaf.on('test:foo', function(e) {
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.areEqual(0, count, 'leaf.on should be first');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
8c2f489baa38f019b839e9e6c460386af42c588dJohn Smith count++;
});
branch.on('test:foo', function() {
Y.Assert.areEqual(1, count, 'branch.on should be second');
Y.Assert.isNull(called, 'branch.on should be executed before any default function');
count++;
});
root.on('test:foo', function() {
Y.Assert.areEqual(2, count, 'root.on should be third');
Y.Assert.isNull(called, 'root.on should be executed before any default function');
count++;
});
root.after('test:foo', function() {
Y.Assert.areEqual(3, count, 'root.after should be fourth');
Y.Assert.areEqual('root', called, 'root.after should be executed after the root default function');
count++;
});
branch.after('test:foo', function() {
Y.Assert.areEqual(4, count, 'branch.after should be fifth');
Y.Assert.areEqual('branch', called, 'branch.after should be executed after the branch default function');
count++;
});
leaf.after('test:foo', function(e) {
Y.Assert.areEqual(5, count, 'leaf.after should be sixth and last');
Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the leaf default function');
count++;
});
leaf.fire('test:foo', {}, leaf);
Y.Assert.areEqual(6, count);
},
// SRC, ON
// BUBBLE, ON
// SRC, DEFAULT BEHAVIOR
// BUBBLE, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
// SRC, AFTER
// BUBBLE, AFTER
___testAlternativeSequencePost300GA: function() {
var count = 0,
called = null,
fn = function() {
called = this.name;
},
config = {
emitFacade: true,
bubbles: true
},
leaf = new Y.EventTarget(config),
branch = new Y.EventTarget(config),
root = new Y.EventTarget(config);
leaf.name = 'leaf';
branch.name = 'branch';
root.name = 'root';
leaf.addTarget(branch);
branch.addTarget(root);
leaf.publish('test:foo', { defaultFn: fn});
branch.publish('test:foo', { defaultFn: fn});
root.publish('test:foo', { defaultFn: fn});
leaf.on('test:foo', function(e) {
Y.Assert.areEqual(0, count, 'leaf.on should be first');
Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
count++;
});
branch.on('test:foo', function() {
Y.Assert.areEqual(1, count, 'branch.on should be second');
Y.Assert.isNull(called, 'branch.on should be executed before any default function');
count++;
});
root.on('test:foo', function() {
Y.Assert.areEqual(2, count, 'root.on should be third');
Y.Assert.isNull(called, 'root.on should be executed before any default function');
count++;
});
leaf.after('test:foo', function(e) {
Y.Assert.areEqual(3, count, 'leaf.after should be fourth');
Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
count++;
});
branch.after('test:foo', function() {
Y.Assert.areEqual(4, count, 'branch.after should be fifth');
Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
count++;
});
root.after('test:foo', function() {
Y.Assert.areEqual(5, count, 'root.after should be sixth and last');
Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
count++;
});
leaf.fire('test:foo', {}, leaf);
Y.Assert.areEqual(6, count);
}
});
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>