5
<title>Example: Loading Tab Content</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: Loading Tab Content</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example shows how to create a plugin to load YQL data into a TabView for dynamic content.</p>
24
<div class="example yui3-skin-sam">
31
<h3>Today's Browser News</h3>
33
<script type="text/javascript">
34
YUI().use('tabview', 'yql', function(Y) {
35
// YQL plugin for Y.Tab instances
36
var TabYQL = function(config) {
40
TabYQL.NS = 'yql'; // plugin namespace (e.g. "tab.yql.load(myQuery)");
43
init: function(config) {
45
this.tab = config.host;
46
this.query = config.query || this.query;
47
this.errorMsg = config.errorMsg || this.errorMsg;
51
this.tab.after('selectedChange', Y.bind(this.afterSelectedChange, this));
57
errorMsg: 'There was a problem loading the content',
60
afterLoad: function(response) {
61
var results = (response.query) ? response.query.results.item : null,
65
Y.each(results, function(fields) {
66
content += '<li><a href="' + fields.link + '">' +
67
fields.title + '</a></li>';
71
content = '<ul>' + content + '</ul>'
73
content = this.errorMsg;
76
this.tab.set('content', content);
79
afterSelectedChange: function(e) {
80
// only load if not already loaded
82
this.load(this.query, this.afterLoad);
86
load: function(query, afterLoad) {
87
Y.YQL(query, Y.bind(afterLoad, this));
91
var tabview = new Y.TabView(),
93
Chrome: 'chrome+browser',
94
Firefox: 'firefox+browser',
95
Safari: 'safari+browser',
96
Explorer: 'explorer+browser',
97
Opera: 'opera+browser'
100
Y.each(feeds, function(feed, label) {
101
var tab = new Y.Tab({
103
content: 'loading...'
107
query: 'select title, link from rss where ' +
108
'url="http://search.news.yahoo.com/rss?p=' +
115
tabview.render('#demo');
121
<h2>Creating the YQL Plugin</h2>
122
<h3>Plugin Constructor</h3>
123
<p>To create a plugin, we need to create a constructor with a static
124
<code>NS</code> property. This is the namespace used by the plugin on each
127
<pre class="code prettyprint">// YQL plugin for Y.Tab instances
128
var TabYQL = function(config) {
132
TabYQL.NS = 'yql'; // plugin namespace (e.g. "tab.yql.load(myQuery)");</pre>
135
</h3>Plugin Prototype</h3>
136
<p>Next we will add the YQL functionality to the prototype. The init method
137
wires the YQL functionality up using the load method.</p>
139
<pre class="code prettyprint">TabYQL.prototype = {
140
init: function(config) {
142
this.tab.after('selectedChange', Y.bind(this.afterSelectedChange, this));
146
afterSelectedChange: function(e) {
147
// only load if not already loaded
149
this.load(this.query, this.afterLoad);
153
load: function(query, afterLoad) {
154
Y.YQL(query, Y.bind(afterLoad, this));
158
<h2>Creating the TabView</h2>
159
<p>Next we will create a new instance of a TabView:</p>
161
<pre class="code prettyprint">/* Create a new TabView instance, with content generated from script */
162
var tabview = new Y.TabView();</pre>
165
<p>And then use the <code>add</code> method to add the <code>Tab</code> instances,
166
to add a tab for each of the feeds, then render the tabview into the placeholder
170
<pre class="code prettyprint">var feeds = {
171
Chrome: 'chrome+browser',
172
Firefox: 'firefox+browser',
173
Safari: 'safari+browser',
174
Explorer: 'explorer+browser',
175
Opera: 'opera+browser'
178
Y.each(feeds, function(feed, label) {
179
var tab = new Y.Tab({
181
content: 'loading...',
185
query: 'select title, link from rss where ' +
186
'url="http://search.news.yahoo.com/rss?p=' +
187
feed + '"'
193
tabview.render('#demo');</pre>
198
<div id="sidebar" class="yui3-u">
202
<div class="sidebox">
204
<h2 class="no-toc">Examples</h2>
208
<ul class="examples">
211
<li data-description="This example shows how to create a TabView wigdet from existing HTML.">
212
<a href="tabview-basic.html">TabView from Existing Markup</a>
217
<li data-description="This example shows how to create a TabView wigdet from JavaScript.">
218
<a href="tabview-fromjs.html">Dynamic TabView from JavaScript</a>
223
<li data-description="This example shows how to add and remove Tabs.">
224
<a href="tabview-add-remove.html">Adding and Removing Tabs</a>
229
<li data-description="This example shows how to load tab content remotely using a YQL plugin.">
230
<a href="tabview-yql.html">Loading Tab Content</a>
242
<div class="sidebox">
244
<h2 class="no-toc">Examples That Use This Component</h2>
248
<ul class="examples">
259
<li data-description="Demonstrates how to add browser history support to a TabView widget using the History Utility.">
260
<a href="../history/history-tabview.html">History + TabView</a>
272
<script src="../assets/vendor/prettify/prettify-min.js"></script>
273
<script>prettyPrint();</script>