PostNuke: A Flexible Open Source Content Management System
home | forum | international support | contact us

Documentation Wiki

Ajax files in the Core


  • includes/pnobjlib/AjaxUtil.class.php
  • javascript/ajax/*
  • system/pnRender/plugins/function.pnajaxheader.php
  • ajax.php


AjaxUtil.class.php


A utility class to be used in your pnajax.php offering several useful functions:

AjaxUtil::output()


    /**
     * encode data in JSON and return
     * This functions can add a new authid if requested to do so (default).
     * If the supplied args is not an array, it will be converted to an
     * array with 'data' as key.
     * Authid field will always be named 'authid'. Any other field 'authid'
     * will be overwritten!
     * Script execution stops here
     *
     * @param args - string or array of data
     * @param createauthid - create a new authid and send it back to the calling javascript
     * @param xjsonheader - send result in X-JSON: header for prototype.js
     * @author Frank Schummertz
     *
     */

AjaxUtil::error()


    /**
     * error()
     *
     * Immediately stops execution and returns an error message
     *
     * @param error - error text
     * @param code - optional error code, default '400 Bad data'
     * @author Frank Schummertz
     *
     */

AjaxUtil::convertToUTF8()


    /**
     * convertToUTF8()
     * converts a string or an array (recursivly) to utf-8
     *
     * @param input - string or array to convert to utf-8
     * @return converted string or array
     * @author Frank Schummertz
     *
     */

AjaxUtil::convertFromUTF8()


    /**
     * convertFromUTF8()
     * converts a string from utf-8
     *
     * @param input - string or array to convert from utf-8
     * @return converted string
     * @author Frank Schummertz
     *
     */

javascript/ajax


This folder contains the external libraries

  • prototype.js
  • script.aculo.us libraries
  • behaviour.js

and

  • pnajax.js

For more information about the libraries see framework

The pnajax.js contains some useful public function (this is by no means complete!):

pndejsonize()


    /**
     * pndejsonize
     * unserializes an array
     *
     *@param jsondata JSONized array in utf-8 (as created by AjaxUtil::output
     *@return array
     */

pnsetselectoption


    /**
     * pnsetselectoption
     * sets a select to a given value
     *
     *@param id select id or object
     *@param sel the value that should be selected
     *@return void
     *
     */

pnupdateauthids


    /**
     * pnupdateauthids
     * updates all hidden authid fields with a new authid obtained with an ajax call
     *
     *@param authid the new authid
     *@return void
     *
     */

function.pnajaxheader.php


This pnRender plugin adds all necessary headers for the external libraries plus module specific javascript

Usage


In a template simply use

<!--[ pnajaxheader modname=mymodule filename=mymodule.js ]-->

  • modname: well known module name
  • filename: name of external, modulespecific javascript file, defaults to ajax.js

Output in the head section:

ajax.php


The main entry point for all Ajax functions. In your module send the httprequests to this file, e.g.

ajax.php?module=mymodule&func=dosomething&param=1

type is not necessary, default for this is ajax.

This example results in calling mymodule_ajax_dosomething() function in mymodule/pnajax.php which has to return an associative array at its end. If not, the result is converted to such an array with key 'data'. It automatically adds another entry 'authid' with a fresh authentication id. It is your task to update all hidden authid fields later on using the pnupdateauthid() utility function.
The complete result set (including the authid) is sent as JSON string (a serialized array) which has to be decoded using the pndejsonize() utility function.

Ajax goodies


Automatically show a "work in progress" image


Whenever a httprequest is started, the main Ajax object is trying to show an element with the id 'ajax_indicator'. This is done automatically if such an id exists in your page. Upon completion it gets hidden again.

Ajax timeout


The same function that does the "work in progress" image installs a timeout watchdog: If an Ajax request is not completed within 5 seconds, it gets aborted.


CategoryDeveloperDocs
XML Revisions of $tag
Page history :: Last Editor [ geoff ] :: Owner [ markwest ] ::
Valid XHTML :: Valid CSS :: Powered by pnWikka 1.0 (A wiki fork from WikkaWiki)
 

Main Menu

Extensions Database

Documentation

Development

Login





 


 Log in Problems?
 New User? Sign Up!

Donate to PostNuke