croczilla.com 
 home   stratified   bits&pieces   blog   
  home > oni > Ajax

Oni Library for JavaScript AJAX Programming

Overview

oni-ajax.js is a library for client-side Oni AJAX programming which builds upon the core Oni abstractions provided by oni.js. Using oni-ajax.js in a html page requires inclusion of both of these files:

<script type="text/javascript" src="../../oni.js"></script>
<script type="text/javascript" src="../oni-ajax.js"></script>

Just like oni.js, oni-ajax.js should work in all major browsers. Please report any incompatibilites you might come across to the Oni mailinglist.

Note that oni-ajax.js is a work in progress, just like the core Oni language (or even more so). It is very likely that the library will change in backward-incompatible ways over time.


DOM-related functions

Functions related to the DOM. All of these functions are namespaced to the JavaScript object 'DOM'.


'DOM.WaitForEvent'

An Oni function with signature

DOM.WaitForEvent(event_exp, elem_exp)

Adds an event listener listening for DOM event <event_exp> on DOM element <elem_exp>. Blocks until the event fires, then deregisters the event listener and returns the event object.

If the execution is cancelled before the event fires (e.g. by an enclosing Alt), the event listener will be deregistered.


'DOM.ElementById'

Oni equivalent of document.getElementById(.).

An Oni function with signature

DOM.ElementById(id_exp)

Returns document.getElementById(<id_exp>).


'DOM.Alert'

Oni equivalent to window.alert(.).

An Oni function with signature

DOM.Alert(message_exp)

Executes window.alert(<message_exp>) and returns immediately. The return value is undefined.


'DOM.LoadScript'

Load a JavaScript file into the current document.

An Oni function with signature

DOM.LoadScript(url_exp)

The first time DOM.LoadScript is called for a particular <url_exp>, a script-element will be inserted into the DOM with src set to <url_exp>, and the expression will block until the script element has finished loading.

On subsequent invocations of DOM.LoadScript for a particular <url_exp>, the script will not be reloaded. The expression will return immediately if the first load has finished already, or, if the first load is still pending, it will block until the load finishes.


Google APIs

These functions are wrappers around the Google AJAX APIs. They can be called without first calling any initialization code or loading any google-specific JS files into the DOM beforehand; the relevant Google APIs will be initialized implicitly.

The Google AJAX APIs use the "script tag hack" to get around browsers' cross-site restrictions. This means that no special server side support is needed to use these APIs: webpages can directly access the services at Google's servers, no matter which domain they are being loaded from.


'Google.EnsureAPI'
Google.EnsureAPI()

Ensures that the Google loader script http://www.google.com/jsapi is loaded into the page.

This is called implicitly inside the library most of the time. The only reason you would want to call this is to ensure that datastructures such as google.loader.ClientLocation are initialized.


'Google.Load'

The Oni version of google.load().

Google.Load(modulename_exp, moduleversion_exp)

Blocks until the Google API module <modulename_exp>, version <moduleversion_exp> has been loaded.

This is called implicitly inside the library most of the time.


'Google.Feeds.Load'

Retrieve RSS feeds.

Google.Feeds.Load(url_exp [, entry_count_exp])

Retrieve rss feed at <url_exp> using the Google AJAX Feeds API. The optional argument <entry_count_exp> specifies the maximum number of feed items to be returned (default: 20).

Returns a result object as described at http://code.google.com/apis/ajaxfeeds/documentation/reference.html.


'Google.Language.Translate'

Translate text from one language to another.

Google.Language.Translate(text_exp, srclang_exp, destlang_exp
                          [, contenttype_exp])

Translates <text_exp> from language <srclang_exp> to language <destlang_exp>, with the optional argument <contenttype_exp> specifying the content type of text (default: 'html'). See http://code.google.com/apis/ajaxlanguage/documentation/ for valid values for the parameters.

If the underlying google.language.translate call succeeds, a translation result object will be returned.

If the underlying google.language.translate call fails (i.e. it returns an error result), an exception with tag "error" will be thrown. The exception's value will be set to the translation result object's 'error' member.

If the underlying google.language.translate call doesn't call back on its callback, the call to Google.Language.Translate will not return.

Google.Language.Translate implements some enhancements over the corresponding Google API function. E.g. the Oni version chunks up text, so it can be used to translate arbitrarily long texts (arbitrary meaning: bounded by the js engine, and not the 2000 character URL limit of the base Google API function).


'Google.Language.Detect'
Google.Language.Detect(text_exp)

Tries to detect the language of the text <text_exp>.

If the underlying google.language.detect call succeeds, a language detection result object will be returned.

If the underlying google.language.detect call fails (i.e. it returns an error result), an exception with tag "error" will be thrown. The exception's value will be set to the translation result object's 'error' member.

If the underlying google.language.detect call doesn't call back on its callback, the call to Google.Language.Detect will not return.


'Google.Language.GetBranding'
Google.Language.GetBranding(elem_exp [, options_exp])

Appends Google attribution branding to DOM element <elem_exp>. <options_exp> is an optional options map, as described in the Google documentation for google.language.getBranding.


More soon.