Results 1 to 2 of 2

Thread: [Javascript API] How to call webservices ?

  1. #1
    ychaouche is offline Senior Member
    Join Date
    Jul 2012
    Location
    Algiers
    Posts
    55
    Rep Power
    3

    Default [SOLVED] How to call external webservices ?

    Hi.

    I can't find any good documentation about how to call webservices in javascript.

    I found API entries for sendRequest and AjxCallback but neither shows a code example about how to call webservices using those (if they're all that's needed).

    I also found this RSS Zimlet tutorial Develop an example RSS Zimlet - Zimbra :: Wiki but it doesn't explain enough the webservice part.

    Any suggestion really appreciated.
    Last edited by ychaouche; 09-10-2012 at 02:42 PM.

  2. #2
    ychaouche is offline Senior Member
    Join Date
    Jul 2012
    Location
    Algiers
    Posts
    55
    Rep Power
    3

    Default

    After a few days of battling with it... Now I know it works : you simply call the standard sendRequest method that is already implmented in the ZmZimletBase class.

    This is a complete working example that will show in a dialog the last entries of the RSS feed you set in the source code (this.feed_url, in the constructor).

    The most interresting parts of this code is the sendRequest function call in the singleClick handler and the callback function. All the rest is extra code to handle the display.

    So basically, all you need to do is call this.sendRequest giving it the following arguments :

    - requestString : if you need to pass in some request paramters, it should go here. For example "q=h&s=2043" will generate a url like "http://www.site.com/page?q=h&s=2043";
    - URL : the actual url you want to call, the sendRequest method automatically prefixes it with the proxy URL;
    - requestHeaders : this can be null;
    - a callback created via new AjxCallback() : this is just a wrapper around the function you want to call AFTER your ajax call returns. AjxCallback takes one, two or three arguments.

    o One argument : the function to call;
    o Two arguments : the object to bind "this" to when calling the function and the function itself
    o Three arguments : the object to bind "this" to, the function to call, and the arguments to pass to that function, third argument is always an array.


    The function you define as callback will always get at least one argument, which is the XML response from the Ajax request. If an error occurs, sendRequest handles it for you so you don't need to bother (an error dialog opens).

    Code:
    yrss = function(){
        //this.feed_url = "http://www.reddit.com/r/LifeProTips/.rss";
        this.feed_url = "http://sebsauvage.net/rss/updates.xml";
        this._callback = AjxCallback(this,this.callback);
        this.dialog = this._createDialog(); // Créer un dialog réutilisable
    };
    yrss.prototype = new ZmZimletBase();
    yrss.prototype.contructor = yrss;
    
    yrss.prototype._createDialog = function(){
        // le dialog que nous allons utiliser
        var _dialog = appCtxt.getMsgDialog();
        // petite helper function;
        _dialog.setInfo = function(msg) {
    	// "this" inside this function is bound to this.dialog
    	this.setMessage(msg,DwtMessageDialog.INFO_STYLE);
        };
        _dialog.showInfo = function(msg){
    	this.reset();
    	this.setInfo(msg);
    	this.popup();
        };
        return _dialog;
    };
    
    yrss.prototype.singleClicked = function() {
        this.sendRequest("",this.feed_url,null,new AjxCallback(this,this.callback));
    };
    
    _response = null;
    
    yrss.prototype.callback = function(response){
        _response = response;
        var html 	    = "";
        var item 	    = null;
        var description = null;
        var date        = null;
        var link        = null;
        var title       = null;
        var items       = response.xml.getElementsByTagName("item");
        
        for (var i=0;i<items.length;i++){
    	item           = items[i];
    	item.get_value = get_value;
    	description    = item.get_value("description");
    	date           = item.get_value("pubDate");
    	link           = item.get_value("link");
    	title          = item.get_value("title");
    	html          += "<em>"+date+"</em> <a target='_blank' href='"+link+"'>"+title+"</a><br/>"
        }
        self.dialog.showInfo(html);
    
    };
    
    
    // helper function
    get_value = function(node_name){
        // get the value of a single leaf node;
        return this.getElementsByTagName(node_name)[0].firstChild.nodeValue;
    };
    it's a panel zimlet

    Code:
    <zimlet name="yrss" version="0.1" description="Exemple d'appels de webservice en REST">
      <include>zho.js</include>
      <includeCSS>style.css</includeCSS>
      <handlerObject>yrss</handlerObject>
      <zimletPanelItem icon="rss-icon" label="Y!Rss">
        
      </zimletPanelItem>
    </zimlet>
    Last edited by ychaouche; 09-11-2012 at 08:44 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Click-to-call
    By deborahxxx in forum Installation Help
    Replies: 0
    Last Post: 03-26-2011, 02:46 PM
  2. How to call a normal javascript in the zimlet?
    By anuradha_mihsra in forum Zimlets
    Replies: 1
    Last Post: 01-19-2009, 07:43 AM
  3. can't call with skype ?
    By Urs in forum Zimlets
    Replies: 5
    Last Post: 03-11-2008, 08:58 AM
  4. Transaction support for WebServices ?
    By marinew in forum Developers
    Replies: 0
    Last Post: 01-25-2008, 06:32 AM
  5. administration by webservices
    By ghand11 in forum Administrators
    Replies: 0
    Last Post: 08-06-2007, 01:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •