This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.
Tips and Tricks in Developing Gadgets
Here are some tips and tricks to keep in mind, when developing gadgets.
Error Handling
In our sample gadget developed so far errors are not handled. For example, what would happen if the content from URL is not fetched due to a network outage? As of now, we display an empty gadget. It is recommended to display a meaningful error to the userusing a simple try/catch block as follows:
<?xml version="1.0" encoding="UTF-8" ?> <Module> Â Â Â <ModulePrefs title="WSO2 Open Source Development Data from MarkMail"> Â Â Â Â Â Â Â <Require feature="dynamic-height" /> Â Â Â </ModulePrefs> Â Â Â <Content type="html"> Â <![CDATA[ Â <a href="http://markmail.org/"><img src="http://markmail.org/images/logo_white.gif" alt="MarkMail"/></a> Â <div id="contentDiv"></div> Â <span id="hiddenSpan" style="display:none;"/> Â <script type="text/javascript"> Â Â Â Â Â var url = "http://markmail.org/browse/org.wso2.carbon-commits"; Â Â Â Â Â function getHtml() {Â Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â var parameters = {}; Â Â Â Â Â Â Â Â Â parameters[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT; Â Â Â Â Â Â Â Â Â gadgets.io.makeRequest(url, processResponse, parameters); Â Â Â Â Â Â Â } catch (err) { Â Â Â Â Â Â Â Â Â Â Â Â document.getElementById('contentDiv').innerHTML = "<stong> error connecting to URL <a href='" + url +"'>" + url + "</a> </strong>"; Â Â Â Â Â Â Â } Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â function processResponse(response) { Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â document.getElementById('hiddenSpan').innerHTML = response.text; Â Â Â Â Â Â Â Â Â Â document.getElementById('contentDiv').innerHTML = "<table>" + document.getElementById('browse').getElementsByTagName('table')[0].innerHTML + "</table>"; Â Â Â Â Â Â Â } catch (err) { Â Â Â Â Â Â Â Â Â Â document.getElementById('contentDiv').innerHTML = "<stong> error processing data from URL <a href='" + url +"'>" + url + "</a> </strong>"; Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â gadgets.window.adjustHeight(); Â Â Â Â Â }; Â Â Â Â Â Â gadgets.util.registerOnLoadHandler(getHtml); Â Â </script> Â ]]> Â Â Â </Content> </Module>
Incremental Development
Like we did in this tutorial, it is a good practice to start small and incrementally keep on building the gadget, step by step, testing along the way. Incremental testing is key to success. It allows you space to back track and recollect on what went wrong and fix those. If we are to take big steps, it would be harder to pinpoint problem cases.
When Things Go Wrong: F12
When things are not working, the best way to debug is to use developer tools offered by the browsers. The function key F12 brings up the developer tools in both Chrome and Internet Explorer. The console of the developer tools leads you to the errors and error locations easily.
In addition to developer tools, the "Inspect element" option you get on the right click pop-up on Chrome Web browser is quite handy. It is paarticularly helpful when you know where in the gadget content is supposed to appear yet does not.
Always Test with IE
Quite often, if it works in IE, chances are high it works in other browsers. Since you are developing for a large audience, it is important to consider cross-browser compatibility. If you plan to open up the gadgets for a wider audience, it is important to use IE as part of the incremental test efforts. In other words, test with IE while you are developing, rather than trying to test the finished gadget with IE.
Use an IDE
Even simple things like indentation help a great deal when you are developing a fairly complex gadget. Using an IDE is a best practice. You can see how WSO2 Carbon Studio can help you develop gadgets from scratch in tutorial How to Creat Gadgets Using Carbon Studio.