|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhat you will get from this article:
What you will not get from this article:
BackgroundMost gadgets that deal with remote data have three basic states: loading, loaded, and connection error. For the sake of simplicity I have intentionally ignored docked and undocked states.
When I set out to develop my first gadget, I was unhappy with how much code I needed to write to support displaying states. The code I had written seemed messy and more functional than object oriented. To solve this problem I created a base class that provides support for rendering display modes and encapsulates some of the basic gadget API. Using the CodeCreating the HTML for your GadgetBe sure to reference the MicrosoftAjax.js and Dowds.Gadgets.js files first in your gadget's HTML file. <head>
<script type="text/javascript" src="Scripts/MicrosoftAjax.debug.js">
</script>
<script type="text/javascript" src="Scripts/Dowds.Gadgets.js">
</script>
<script type="text/javascript"
src="Scripts/##You_Gadgets_Javasctipt_File##.js"></script>
</head>
Instead of dynamically generating the HTML for each state the
Each <body>
<div id="dataLoadedDisplay" style="position:absolute;visibility:hidden">
<table id="movieListings">
<tr><td class="percent"></td><td class="title"><div><a></a></div>
</td></tr>
...
</table>
</div>
<div id="dataLoadingDisplay" style="position:absolute;visibility:hidden">
<img src="Images/Icon_Spinner.gif" /> Getting Data…
</div>
<div id="dataNotAvailableDisplay"
style="position:absolute;visibility:hidden">
<img src="Images/Icon_Info.png" /> Service Not Available
</div>
</body>
Creating the JavaScript for Your GadgetCreate a new class using ASP.NET AJAX Extensions. Use the Dowds.Gadgets.Movies = function()
{
Dowds.Gadgets.Movies.initializeBase(this);
this._data = null;
};
Dowds.Gadgets.Movies.registerClass("Dowds.Gadgets.Movies",
Dowds.Gadgets.Gadget);
Accessing remote data such as an XML file or a web service can easily be accomplished using the Dowds.Gadgets.Movies.prototype =
{
_requestData: function()
{
// Use ASP.NET Extensions to access the remote XML or web service
var wRequest = new Sys.Net.WebRequest();
wRequest.set_url
("http://i.rottentomatoes.com/syndication/rss/top_movies.xml");
wRequest.add_completed(Function.createDelegate
(this, this._processData));
wRequest.invoke();
}
}
Once the data is returned you'll need to call the
Before calling the Dowds.Gadgets.Movies.prototype =
{
...
_processData: function(executor)
{
if (executor.get_responseAvailable())
{ // The data returned
var movieDoc = executor.get_xml();
this._dataLoaded = true;
this._dataNotAvailable = false;
this._data = Dowds.Gadgets.MovieInfo.ParseFromDoc(movieDoc);
// Update the display HTML for the loaded state
this._bindTable(this._data);
}
else
{ // There was an error connecting to the data
this._dataLoaded = false;
this._dataNotAvailable = true;
}
// Refresh the data in 4 hours
setTimeout(Function.createDelegate(this, this._requestData),
(4 * 3600000));
this._updateDisplay();
}
}
The Opening a FlyoutIf you open a flyout that is already open it loses focus and becomes grayed out. To avoid this, check and see if the flyout is already open and if it is, just refresh the data. _link_onclick: function(e)
{
...
if(System.Gadget.Flyout.show)
{
this._updateFlyout();
}
else
{
System.Gadget.Flyout.show = true;
}
...
},
_updateFlyout: function()
{
var flyoutWindow = System.Gadget.Flyout.document.parentWindow;
flyoutWindow.moviesFlyout.reload(this._selectedMovieInfo);
}
Final ThoughtsIt's very important that you avoid making your gadget look like something a developer designed. Try thinking of an object or metaphor you could use to visually represent your gadget. For example, I created a movie review gadget so a clap board is a fitting backdrop. Additional Resources
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||