5
<title>Example: Creating a Widget Plugin</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: Creating a Widget Plugin</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><style scoped>
24
.yui3-widget-content {
25
border:1px solid #000;
29
background: #fff url(../assets/widget/img/ajax-loader.gif) no-repeat center center;
35
<p>This example shows how you can use Widget's plugin infrastructure to add additional features to an existing widget.</p>
37
We use the <code>"gallery-widget-io"</code> plugin to add io capabilities bound to a widget instance. The plugin provides an <code>io</code> interface on Widget, which can be used to update
38
the widget's contentBox contents.
41
NOTE: This example uses io-xdr to retrieve content from pipes, and requires Flash.
47
<script type="text/javascript">
48
YUI().use("widget", "gallery-widget-io", "json-parse", "substitute", "escape", function(Y) {
50
var formatRSS = function (val) {
51
var formatted = "Error parsing feed data";
53
var json = Y.JSON.parse(val);
55
if (json && json.count) {
56
var html = ['<ul class="yui3-feed-data">'];
57
var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>';
59
Y.each(json.value.items, function(v, i) {
61
v.title = Y.Escape.html(v.title);
62
v.link = Y.Escape.html(v.link);
63
html.push(Y.substitute(linkTemplate, v));
67
formatted = html.join("");
69
formatted = "No Data Available";
72
formatted = "Error parsing feed data";
77
Y.on('io:xdrReady', function() {
79
var widget = new Y.Widget();
81
widget.plug(Y.Plugin.WidgetIO, {
82
uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us',
89
loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">'
91
widget.render('#demo');
99
src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime()
107
<h2>Using The Gallery IO Plugin For Widget</h2>
109
<h3>Setting Up The YUI Instance</h3>
111
<p>For this example, we'll pull in <code>widget</code>; the <code>gallery-widget-io</code> plugin, and the <code>json-parse</code> and <code>substitute</code> modules to help us work with the JSON RSS data returned.
112
The code to set up our sandbox instance is shown below:</p>
114
<pre class="code prettyprint">YUI().use("widget", "gallery-widget-io", "json-parse", "substitute", function(Y) {
115
// We'll write our code here, after pulling in the default Widget module,
116
// the IO plugin.
120
<h3>The Widget IO Plugin</h3>
122
<p>The Widget IO plugin is a fairly simple plugin. It provides incremental functionality. It does not need to modify the behavior of any methods on the host Widget instance, or monitor any Widget events (unlike the <a href="../overlay/overlay-anim-plugin.html">AnimPlugin</a> example).</p>
124
<p>It sets up the following attributes, which are used to control how the IO plugin's <code>refresh</code> method behaves:</p>
128
<dd>The uri to use for the io request</dd>
130
<dd>The io configuration object, to pass to io when initiating a transaction</dd>
132
<dd>The formatter to use to formatting response data. The default implementation simply passes back the response data passed in, unchanged.</dd>
134
<dd>The default content to display while an io transaction is in progress</dd>
137
<h3>Using the Plugin</h3>
139
<p>All objects derived from <a href="http://yuilibrary.com/yui/docs/api/Base.html">Base</a> are <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html">Plugin Hosts</a>.
140
They provide <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html#method_plug"><code>plug</code></a> and <a href="http://yuilibrary.com/yui/docs/api/Plugin.Host.html#method_unplug"><code>unplug</code></a> methods to allow users to add/remove plugins to/from existing instances.
141
They also allow the user to specify the set of plugins to be applied to a new instance, along with their configurations, as part of the constructor arguments.</p>
143
<p>In this example, we'll create a new instance of a Widget:</p>
145
<pre class="code prettyprint">/* Create a new Widget instance, with content generated from script */
146
var widget = new Y.Widget();</pre>
149
<p>And then use the <code>plug</code> method to add the <code>WidgetIO</code> plugin,
150
providing it with a configuration to use when sending out io transactions
151
(The <a href="../overlay/overlay-anim-plugin.html">Animation Plugin</a> example shows how
152
you could do the same thing during construction), render the widget, and refresh
153
the plugin to fetch the content.</p>
155
<pre class="code prettyprint">/*
156
* Add the Plugin, and configure it to use a news feed uri, and work cross-domain, using xdr
158
widget.plug(Y.Plugin.WidgetIO, {
159
uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us',
162
use:'flash'
165
formatter: formatRSS,
166
loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">'
169
widget.render('#demo');
171
/* fetch the content */
172
widget.io.refresh();</pre>
175
<p>We pass in a formatter to the io plugin, which is responsible for translating the JSON RSS from the uri to HTML:</p>
177
<pre class="code prettyprint">var formatRSS = function (val) {
178
var formatted = "Error parsing feed data";
180
var json = Y.JSON.parse(val);
182
if (json && json.count) {
183
var html = ['<ul class="yui3-feed-data">'];
184
var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>';
186
Y.each(json.value.items, function(v, i) {
188
v.title = Y.Escape.html(v.title);
189
v.link = Y.Escape.html(v.link);
190
html.push(Y.substitute(linkTemplate, v));
193
html.push("</ul>");
194
formatted = html.join("");
196
formatted = "No Data Available";
199
formatted = "Error parsing feed data";
205
<p>NOTE: Since we're using <code>IO</code>'s XDR functionality for this example, we wrap the widget construction in a callback which notifies us when the flash XDR module is ready to service requests. In your local implementations,
206
if you're not sending cross-domain requests, you don't need to use the XDR functionality and leave out the code below:</p>
208
<pre class="code prettyprint">Y.on('io:xdrReady', function() {
209
// Setup Widget when io xdr is available
212
// Set up io to use the flash XDR transport
214
id:'flash',
216
src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime()
220
<h2>Complete Example Source</h2>
221
<pre class="code prettyprint"><div id="demo"></div>
222
<script type="text/javascript">
223
YUI().use("widget", "gallery-widget-io", "json-parse", "substitute", "escape", function(Y) {
225
var formatRSS = function (val) {
226
var formatted = "Error parsing feed data";
228
var json = Y.JSON.parse(val);
230
if (json && json.count) {
231
var html = ['<ul class="yui3-feed-data">'];
232
var linkTemplate = '<li><a href="{link}" target="_blank">{title}</a></li>';
234
Y.each(json.value.items, function(v, i) {
236
v.title = Y.Escape.html(v.title);
237
v.link = Y.Escape.html(v.link);
238
html.push(Y.substitute(linkTemplate, v));
241
html.push("</ul>");
242
formatted = html.join("");
244
formatted = "No Data Available";
247
formatted = "Error parsing feed data";
252
Y.on('io:xdrReady', function() {
254
var widget = new Y.Widget();
256
widget.plug(Y.Plugin.WidgetIO, {
257
uri : 'http:/' + '/pipes.yahooapis.com/pipes/pipe.run?_id=6b7b2c6a32f5a12e7259c36967052387&_render=json&url=http:/' + '/rss.news.yahoo.com/rss/us',
260
use:'flash'
263
formatter: formatRSS,
264
loading: '<img class="yui3-loading" width="32px" height="32px" src="../assets/widget/img/ajax-loader.gif">'
266
widget.render('#demo');
272
id:'flash',
274
src:'../../build/io-xdr/io.swf?stamp=' + (new Date()).getTime()
278
</script></pre>
283
<div id="sidebar" class="yui3-u">
287
<div class="sidebox">
289
<h2 class="no-toc">Examples</h2>
293
<ul class="examples">
296
<li data-description="Shows how to extend the base widget class, to create your own Widgets.">
297
<a href="widget-extend.html">Extending the Base Widget Class</a>
302
<li data-description="Shows how to use Base.create and mix/match extensions to create custom Widget classes.">
303
<a href="widget-build.html">Creating Custom Widget Classes With Extensions</a>
308
<li data-description="Shows how to create an IO plugin for Widget.">
309
<a href="widget-plugin.html">Creating a Widget Plugin</a>
314
<li data-description="Shows how to extend the Widget class, and add WidgetPosition and WidgetStack to create a Tooltip widget class.">
315
<a href="widget-tooltip.html">Creating a Simple Tooltip Widget With Extensions</a>
320
<li data-description="Shows how to extend the Widget class, and add WidgetParent and WidgetChild to create a simple ListBox widget.">
321
<a href="widget-parentchild-listbox.html">Creating a Hierarchical ListBox Widget</a>
335
<script src="../assets/vendor/prettify/prettify-min.js"></script>
336
<script>prettyPrint();</script>