AJAX. Javascript Function, XML Service

If you have found a pretty good javascript control that requires data to be loaded from server asynchronously – XsltDb is something you’d appreciate. It provides a very simple way to load data asynchronously by javascript function or by providing a URL.

Here I provide an example of loading list of Modules currently installed in DotNetNuke.

1. Creating javascript function

The first method covers full cycle of AJAX. XsltDb helps creating javascript function that goes to the server (using XmlHttpRequest) queries database, returns data to client and call a user-defined callback. This is an XsltDb configuration that creates such a function:

<mdo:callable js="getModules(filter)">
   <xsl:copy-of select="mdo:sql('
       select * from {databaseOwner}[{objectQualifier}desktopModules]
       where FriendlyName like @filter
   ', 'module', '@filter', concat('%', mdo:request(filter), '%'))" />
</mdo:callable>

The code is pretty short, isn’t it?

Attention! As we use free sql statement we must check the Super Module option above XSLT Editor.

Resulting output on HTML page is a javascript function getModules that returns a list of modules as XML string. You can use XSL to create any type of output: string, JSON, etc. Usage of the getModules is simple:

Asynchronous

getModules("x", function(result){
   alert(result);
});

Or synchronous (not recommended)

alert(getModules("x"));

2. Creating a URL handler

Controls often capable of querying a URL that return XML data. This is also easy to implement with XsltDb. Create a new module, click Edit XSLT and fill “Alias” field above the control with “modules” alias name. You also must check Super Module option tobe able to execute SQL.

<xsl:if test=" mdo:request('service')='modules' ">
   <xsl:copy-of select="mdo:sql('
       select * from {databaseOwner}[{objectQualifier}desktopModules]
       where FriendlyName like @filter
  ', 'module', '@filter', concat('%', mdo:request(filter), '%'))" />
</xsl:if>

xsl:if is not nesessary and used to prevent output generation when we open a page where the module sits.

The URL that returns data provided by the module is:

/DesktopModules/XsltDb/ws.aspx?service=alias-name[&additional-parameters]

In our case we must use

/DesktopModules/XsltDb/ws.aspx?service=modules&filter=x

Example would be:

$.ajax({
      url: '/DesktopModules/XsltDb/ws.aspx?service=modules&filter=x',
      success: function(data, status, http) {
      alert(http.responseText);
   }
});

Note that you can put all your services on a separate page and reuse it from any page of your site or from other sites.

More about AJAX in XsltDb:

Advertisements

About Anton Burtsev

Develop software since 2000
This entry was posted in DotNetNuke, XsltDb. Bookmark the permalink.

2 Responses to AJAX. Javascript Function, XML Service

  1. Nice!! XsltDb is awesome!!
    I will use it to create a simple radio database (with filters like music type) and play the MMS URL Streaming using JWPlayer (Silverlight Version which accepts reproducing as an external WMPlayer)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s