Data
The Data class provides abstract methods to access external resources using Ajax (XMLHttpRequest) requests.
Credits
Partially based on MooTools, My Object Oriented Javascript Tools. Copyright © 2006-2007 Valerio Proietti, http://mad4milk.net, MIT Style License. Partially based on Prototype JavaScript framework, version 1.6.0 © 2005-2007 Sam Stephenson. Prototype is freely distributable under the terms of an MIT-style license. For details, see the Prototype web site: http://www.prototypejs.org/
| Data | The Data class provides abstract methods to access external resources using Ajax (XMLHttpRequest) requests. |
| methods | |
| Functions | |
| getFeed | Gets the content of a feed, in a JSON format. |
| getXml | This method is used to get the content of an external XML data source. |
| getText | This method is used to get the content of an external data source. |
| getJson | This method is used to get the content of an external JSON data source. |
| getModule | This method is used to get the content of an external widget, in XML format. |
| request | This method is used to get the content of an external data source. |
methods
| Functions | |
| getFeed | Gets the content of a feed, in a JSON format. |
| getXml | This method is used to get the content of an external XML data source. |
| getText | This method is used to get the content of an external data source. |
| getJson | This method is used to get the content of an external JSON data source. |
| getModule | This method is used to get the content of an external widget, in XML format. |
| request | This method is used to get the content of an external data source. |
getFeed
getFeed: function( url, callback )
Gets the content of a feed, in a JSON format.
Parameters
- String url: the URL of the feed data source.
- Function callback: the callback method that will be triggered when the request is succesful. This method must have one parameter to receive the feed (JSON format) returned by the request.
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the feed as parameter.
Example
UWA.Data.getFeed('http://feeds.feedburner.com/NetvibesDevBlog', myModule.display);
myModule.display = function(aFeed) {
// your display code
}Notes
In this example, the callback method is named “display”, and is used to display the feed content, which is contained in the aFeed variable.
getXml
getXml: function( url, callback )
This method is used to get the content of an external XML data source. It can be used to retrieve the content of a feed in XML format.
Parameters
- String url: the URL of the XML data source,
- Function callback: the callback method that will be fired when the request is succesful. This method must have one parameter to receive the XML content returned by the request.
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the XML content as parameter.
Example
UWA.Data.getXml('http://example.com/content.xml', myModule.parse);
myModule.parse = function(xml) {
// your parsing code
}Notes
In this example, the callback method is named “parse”, and is used to parse the XML tree, which is contained in the “xml” variable.
getText
getText: function( url, callback )
This method is used to get the content of an external data source. It can be used to retrieve any kind of content, as long as it is made of text.
Parameters
- String url: the URL of the data source,
- Function callback: the callback method that will be fired when the request is succesful. This method must have one parameter to receive the text content returned by the request.
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the XML content as parameter.
Example
UWA.Data.getText('http://example.com/content.txt', myModule.parse);
myModule.parse = function(text) {
// your parsing code
}
Notes:
In this example, the callback method is named "parse", and is used to display the feed content, which is contained in the "text" variable.getJson
getJson: function( url, callback )
This method is used to get the content of an external JSON data source. It can be used to retrieve any kind of JSON data.
Parameters
- String url: the URL of the data source,
- Function callback: the callback method that will be fired when the request is succesful. This method must have one parameter to receive the JSON content returned by the request.
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the JSON content as parameter.
Example
UWA.Data.getJson('http://example.com/json.php', myModule.parse);
myModule.parse = function(text) {
// your parsing code
}getModule
getModule: function( url, callback, id )
This method is used to get the content of an external widget, in XML format.
Parameters
- String url: the URL of the feed data source,
- Function callback: the callback method that will be fired when the request is succesful. This method must have one parameter to receive the module content returned by the request.
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the widget’s content as parameter.
request
request: function( url, request )
This method is used to get the content of an external data source. It can be used to retrieve or set any kind of data: text-based, XML, JSON or a feed. The other Ajax methods (getText(), getXml(), getJson(), getFeed()) are all shortcut methods to specific uses of request(). This method is also the only way to perform HTTP POST request, as well as authenticated requests
Parameters
- String url: the URL of the data source.
- Object request: a JavaScript object containing setting/value pairs. This object can take a handful of settings, the only required one being ‘onComplete’, because you need to always set a callback method that will receive the Ajax response. That method must have one parameter to receive the JSON content returned by the request.
Sample request object
{ method : 'get', proxy: 'ajax', type: 'xml', onComplete: callback }Available methods
^ Setting ^ Options ^ Default option method get, post (in lowercase!) post proxy ajax, feed ajax type json, xml, text, html text cache seconds of server caching) undefined onComplete (choose your own method) undefined parameters (your POST parameters) undefined postBody (in case you need to set it) undefined authentication (the auth object. See doc) undefined
Returns
- Nothing, but if the request is successful, the callback method is fired and receives the content as parameter.
Example
UWA.Data.request(
'http://example.org/api.php',
{
method: 'get',
proxy: 'ajax',
type: 'xml',
cache: 3600,
onComplete: myModule.parse
});
myModule.parse = function(response) {
// your parsing code
}Gets the content of a feed, in a JSON format.
getFeed: function( url, callback )
This method is used to get the content of an external XML data source.
getXml: function( url, callback )
This method is used to get the content of an external data source.
getText: function( url, callback )
This method is used to get the content of an external JSON data source.
getJson: function( url, callback )
This method is used to get the content of an external widget, in XML format.
getModule: function( url, callback, id )
This method is used to get the content of an external data source.
request: function( url, request )