Monday 11 January 2010

Error in the Chapter 2 Tutorial

I suppose it serves me right for following the tutorial so blindly and assuming that all the code was correct. My first attempt at doing a webscript (as per tutorial) nearly came to an abrupt end.

In the tutorial when it's talking about setting up the newsfeed script, there are some errors in the javascript and also in the freemarker template.

For newsfeed.get.js, the code should read...

//Define the remove service url                       
var newsServiceUrl = "http://www.renewableenergyworld.com/rss/renews.rss";
//Create an instance of remote connector. We will use http type.
var connector = remote.connect("http");
var re = /^http:\/\//;
if (!re.test(newsServiceUrl))
{
   newsServiceUrl = "http://" + newsServiceUrl;
}
//Make the HTTP connection.
var result = connector.call(newsServiceUrl);

if (result !== null)
{
    //Conver the result into String.

    var rssXml = new String(result);   
    var re = /<[r|R][s|S]{2}/; // Is this really an RSS document?
    //Make sure it is a RSS XML document.
    if (re.test(rssXml))
    {
        model.message = "We got into the processing bit";
        // Strip out any preceding xml processing instructions or E4X will choke
        var idx = rssXml.search(re);
        rssXml = rssXml.substring(idx);
       
        // It looks we need to get rid of the trailing junk as well.
        if ( rssXml.indexOf('') != -1 ) {       
            rssXml = rssXml.substring(0,rssXml.indexOf('')+6);
        }
       
        // Parse the xml document using E4X APIs.
        var rss = new XML(rssXml);
        model.title = rss.channel.title.toString();
        model.items = [];
   
        var item, obj;
        //Loop over all feed items.
        for each (item in rss.channel.item)
        {
           //Retrieve field valuse and populate the JSON object that later will be
           //passed on the the view template.
           obj = {
              "title": item.title.toString(),
              "description": item.description.toString(),
              "link": item.link.toString()
           };
          
           model.items.push(obj);
        }       
       
    }    else {
       
        model.message = "Doesn't work!";
       
    }
}
Notice the "<" arrow in the "re" variable for testing whether its an RSS feed or not. It's was round the wrong way.

Secondly, in the freemaker template, it should be...


  
${title}

  
${message}

  

    <#if items?exists && (items?size > 0)>
        <#list items as item>
            

            
${item.description}

       
   
   
<#-- end of body -->
<#-- end of dashlet -->
 For some reason the "items?size > 0" does not equate to a true/false expression. It needs to be enclosed in ( ) for it to work.

1 comment:

  1. Hi,

    First error was a typo. The second one was due to mis-escaped special characters in the docbook xml.

    Both errors have been fixed and changes have been checked in.

    I also added the source code of the newsfeed component to the tutorial.

    Thanks!

    Yong

    ReplyDelete