<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example: Creating a Universal Console</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<h1>Example: Creating a Universal Console</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><style>
#yconsole {
text-align: right;
}
#demo .first button {
margin-bottom: 2em;
}
#demo .yui3-console .yui3-console-title {
border: 0 none;
color: #000;
font-size: 13px;
font-weight: bold;
margin: 0;
text-transform: none;
}
#demo .yui3-console .yui3-console-entry-meta {
margin: 0;
}
.clr {
clear: both;
}
</style>
<div class="intro">
<p>In this example, three YUI instances are created, each binding a button to call <code>Y.log(...)</code>. Then an additional, separate YUI instance is created that requests only the <code>console</code> module and creates a Console that listens for log events from all three.</p>
</div>
<div class="example yui3-skin-sam">
<div id="demo" class="yui3-skin-sam">
<div class="yui3-g">
<div class="yui3-u-1-2 first">
<h4>YUI #1</h4>
<button type="button" id="yui1">Log from YUI instance #1</button>
<h4>YUI #2</h4>
<button type="button" id="yui2">Log from YUI instance #2</button>
<h4>YUI #3</h4>
<button type="button" id="yui3">Log from YUI instance #3</button>
</div>
<div class="yui3-u-1-2" id="yconsole">
</div>
</div>
<div class="clr"></div>
</div>
<script>
// Create the first YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui1" ).on( "click", function () {
Y.log( "Message from YUI instance #1" );
});
});
// Create the second YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui2" ).on( "click", function () {
Y.log( "I'm the second YUI instance" );
});
});
// Create a third YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui3" ).on( "click", function () {
Y.log( "And this is #3 YUI" );
});
});
// Create a separate YUI instance to listen to all instances' logging
YUI().use("console", function (Y) {
// Configure the Console's logSource to Y.Global to make it universal
new Y.Console({
logSource: Y.Global,
style: 'inline',
newestOnTop: false
}).render( "#yconsole" );
});
</script>
</div>
<h3 class="first">Multiple YUI instances</h3>
<p>Each YUI instance is its own sandbox. You can create as many YUI instances on a page as you want or need (though typically one is enough). The variables, module configurations and instances are kept inside that instance unless you expressly expose them via a global variable.</p>
<pre class="code prettyprint">// Create the first YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui1" ).on( "click", function () {
Y.log( "Message from YUI instance #1" );
});
});
// Create the second YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui2" ).on( "click", function () {
Y.log( "I'm the second YUI instance" );
});
});
// Create a third YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui3" ).on( "click", function () {
Y.log( "And this is #3 YUI" );
});
});</pre>
<h3>Listening to <code>Y.Global</code></h3>
<p>To address debugging such an environment, Console can be configured to listen for log messages across all YUI instances by setting the Console instance's <code>logSource</code> configuration to <code>Y.Global</code>.</p>
<p>We'll insert the universal Console into another YUI instance.</p>
<pre class="code prettyprint">// Create a separate YUI instance to listen to all instances' logging
YUI().use("console", function (Y) {
// Configure the Console's logSource to Y.Global to make it universal
new Y.Console({
logSource: Y.Global,
style: 'block',
newestOnTop: false,
width: "250px"
}).render( "#yconsole" );
});</pre>
<h3>Full Code Listing</h3>
<h4>Markup</h4>
<pre class="code prettyprint"><div id="demo" class="yui3-skin-sam">
<div class="yui3-g">
<div class="yui3-u-1-2 first">
<h4>YUI #1</h4>
<button type="button" id="yui1">Log from YUI instance #1</button>
<h4>YUI #2</h4>
<button type="button" id="yui2">Log from YUI instance #2</button>
<h4>YUI #3</h4>
<button type="button" id="yui3">Log from YUI instance #3</button>
</div>
<div class="yui3-u-1-2" id="yconsole">
</div>
</div>
<div class="clr"></div>
</div></pre>
<h4>JavaScript</h4>
<pre class="code prettyprint"><script>
// Create the first YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui1" ).on( "click", function () {
Y.log( "Message from YUI instance #1" );
});
});
// Create the second YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui2" ).on( "click", function () {
Y.log( "I'm the second YUI instance" );
});
});
// Create a third YUI instance
YUI().use("node", function (Y) {
Y.one( "#yui3" ).on( "click", function () {
Y.log( "And this is #3 YUI" );
});
});
// Create a separate YUI instance to listen to all instances' logging
YUI().use("console", function (Y) {
// Configure the Console's logSource to Y.Global to make it universal
new Y.Console({
logSource: Y.Global,
style: 'inline',
newestOnTop: false
}).render( "#yconsole" );
});
</script></pre>
<h4>CSS</h4>
<pre class="code prettyprint"><style>
#yconsole {
text-align: right;
}
#demo .first button {
margin-bottom: 2em;
}
#demo .yui3-console .yui3-console-title {
border: 0 none;
color: #000;
font-size: 13px;
font-weight: bold;
margin: 0;
text-transform: none;
}
#demo .yui3-console .yui3-console-entry-meta {
margin: 0;
}
.clr {
clear: both;
}
</style></pre>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="The basics of setting up a Console">
<a href="console-basic.html">Creating a Console for Debugging</a>
</li>
<li data-description="Using your YUI instance configuration to filter which messages are reported in the Console">
<a href="console-yui-config.html">YUI Configuration to Filter Log Messages</a>
</li>
<li data-description="Using the Console's logSource attribute to consolidate log messages from multiple YUI instances into one Console">
<a href="console-global.html">Creating a Universal Console</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Adding the ConsoleFilters plugin to a Console instance for more granular run time log message filtering">
<a href="../console-filters/console-filters-intro.html">Using the ConsoleFilters Plugin</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/console',
name: 'console-global',
title: 'Creating a Universal Console',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('console-basic');
YUI.Env.Tests.examples.push('console-yui-config');
YUI.Env.Tests.examples.push('console-global');
YUI.Env.Tests.examples.push('console-filters-intro');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>