Class AjaxResponse

Description

Ajax response builder

This class contains methods that allow to build a sequence of Javascript commands to be executed on the client side, when the request is completed.

Examples:

  1.  function sayHello($params{
  2.    $response new AjaxResponse();
  3.    $response->alert('Hello World!');
  4.    return $response;
  5.  }
  6.  function sum($params{
  7.    $result ($params['a'$params['b']);
  8.    $response new AjaxResponse();
  9.    $response->setValue('result'$result);
  10.    $response->clear(array('a''b'));
  11.    $response->focus('a');
  12.    return $response;
  13.  }
  14.  $service new AjaxService();
  15.  $service->registerHandler('sayHello');
  16.  $service->registerHandler('sum');
  17.  $service->handleRequest();

Located in /core/service/ajax/AjaxResponse.class.php (line 39)

PHP2Go
   |
   --AjaxResponse
Variable Summary
string $charset
array $commands
Method Summary
void addEvent (string $id, string $type, string $func, [bool $capture = TRUE])
void alert (string $msg)
void callFunction (string $func, [array $params = array()], [string $scope = NULL], [int $delay = 0])
void clear (string $id)
void clearByName (string $formId, string $fieldName)
void clearContents (string $id)
void confirm (string $msg, [int $skip = 1])
void create (string $tag, [array $attrs = array()], [array $contents = NULL], [string $parent = NULL])
void disable (string|array $id)
void disableByName (string $formId, string $fieldName)
void enable (string|array $id)
void enableByName (string $formId, string $fieldName)
void fillCombo (string $id, array $options, [bool $clear = TRUE], [mixed $value = NULL])
void focus (string $id)
void focusByName (string $formId, string $fieldName)
void hide (string|array $id)
void insertContents (string $id, string $code, [string $pos = 'bottom'], [bool $evalScripts = FALSE])
void redirect (string $url)
void remove (string $id)
void removeEvent (string $id, string $type, string $func, [bool $capture = TRUE])
void render ([bool $isIframe = FALSE])
void replaceContents (string $id, string $code, [bool $evalScripts = FALSE])
void resetForm (string $id)
void runScript (string $script)
void setAttribute (string $id, string|array $attr, [mixed $value = ''])
void setCharset (string $charset)
void setStyle (string $id, string|array $attr, [mixed $value = ''])
void setValue (string $id, [mixed $value = ''])
void setValueByName (string $formId, string $fieldName, mixed $value)
void setVariable (string $name, mixed $value)
void show (string|array $id)
void updateContents (string $id, string $code, [bool $evalScripts = FALSE], [bool $useDom = FALSE])
Variables
string $charset = 'iso-8859-1' (line 54)

Response charset

  • access: private
array $commands = array() (line 47)

Response commands

  • access: private
Methods
Constructor AjaxResponse (line 61)

Class constructor

AjaxResponse AjaxResponse ()
addEvent (line 583)

Adds an event handler on an element

  1.  $response->addEvent('my_button''click'"alert('Hello World!');");
  2.  $response->addEvent('my_button''click'JSONEncoder::jsIdentifier('function_name'));

void addEvent (string $id, string $type, string $func, [bool $capture = TRUE])
  • string $id: Element ID
  • string $type: Event type
  • string $func: Function
  • bool $capture: Capture flag
alert (line 624)

Shows an alert message

void alert (string $msg)
  • string $msg: Message
callFunction (line 717)

Calls a JS function

  1.  $response->callFunction('myFunction'array(1'str'true)'myObject');

void callFunction (string $func, [array $params = array()], [string $scope = NULL], [int $delay = 0])
  • string $func: Function ID
  • array $params: Function params
  • string $scope: Function scope
  • int $delay: Delay, in miliseconds
clear (line 291)

Clears one or more fields

  1.  $response->clear(array('product_id''amount'));

void clear (string $id)
  • string $id: Field ID or IDs
clearByName (line 313)

Clears a given field given its name and form ID

void clearByName (string $formId, string $fieldName)
  • string $formId: Form ID
  • string $fieldName: Field name
clearContents (line 481)

Clear the contents of an element

  1.  $response->clearContents('error_div');

void clearContents (string $id)
  • string $id: Element ID
confirm (line 647)

Shows a confirmation message

Set skip to 0 to skip all next commands if the user chooses 'no'.

  1.  // skip only next command
  2.  $response->confirm("Are u sure?"1);
  3.  // skip all following commands
  4.  $response->confirm("Click OK to proceed"0);

void confirm (string $msg, [int $skip = 1])
  • string $msg: Message
  • int $skip: Number of commands to skip if user chooses 'no'
create (line 456)

Creates a new element in the DOM tree

  1.  $response->create('input'array('name' => 'text_box''type'=>'text')NULL'my_form');
  2.  $response->create('div'array('id' => 'message')'Hello World!'JSONEncoder::jsIdentifier('document.body'));

void create (string $tag, [array $attrs = array()], [array $contents = NULL], [string $parent = NULL])
  • string $tag: Tag name
  • array $attrs: Element attributes
  • array $contents: Element contents
  • string $parent: Parent node
disable (line 218)

Disables one or more form fields

void disable (string|array $id)
  • string|array $id: Field ID or IDs
disableByName (line 240)

Disables a field given its name and form ID

void disableByName (string $formId, string $fieldName)
  • string $formId: Form ID
  • string $fieldName: Field name
enable (line 183)

Enables one or more form fields

  1.  $response->enable('order_type');
  2.  $response->enable(array('phone''cellphone'));

void enable (string|array $id)
  • string|array $id: Field ID or IDs
enableByName (line 205)

Enables a field given its name and form ID

void enableByName (string $formId, string $fieldName)
  • string $formId: Form ID
  • string $fieldName: Field name
fillCombo (line 160)

Populates a combo box with a given set of options

  1.  $response->fillCombo('sex'array(
  2.    'M' => 'Male',
  3.    'F' => 'Female'
  4.  )true'M');

void fillCombo (string $id, array $options, [bool $clear = TRUE], [mixed $value = NULL])
  • string $id: Combo ID
  • array $options: Combo options
  • bool $clear: If existent options must be removed
  • mixed $value: Combo value
focus (line 257)

Give focus to a given field

  1.  $response->focus('client_name');

void focus (string $id)
  • string $id: Field ID
focusByName (line 274)

Give focus to a given field given its name and form ID

  1.  $response->focus('hire_form''employee_name');

void focusByName (string $formId, string $fieldName)
  • string $formId: Form ID
  • string $fieldName: Field name
hide (line 373)

Hides one or more elements

  1.  $response->hide('product_details');
  2.  $response->hide(array('shopping_cart''add_product'));

void hide (string|array $id)
  • string|array $id: Element ID or IDs
insertContents (line 524)

Insert content on an element

  1.  $response->insertContents('container'"<div>Record saved at " date('r'"</div>"'bottom');

void insertContents (string $id, string $code, [string $pos = 'bottom'], [bool $evalScripts = FALSE])
  • string $id: Element ID
  • string $code: Code
  • string $pos: Insert position (before, top, after or bottom)
  • bool $evalScripts: If scripts must be evaluated
redirect (line 662)

Redirects to a given URL

void redirect (string $url)
  • string $url: URL
remove (line 563)

Removes an element from the tree

  1.  $response->remove('shopping_cart');

void remove (string $id)
  • string $id: Element ID
removeEvent (line 605)

Removes an event handler from an element

void removeEvent (string $id, string $type, string $func, [bool $capture = TRUE])
  • string $id: Element ID
  • string $type: Event type
  • string $func: Function
  • bool $capture: Capture flag
render (line 82)

Renders all response commands as a JSON string

This method is automatically called by AjaxService class when the response is generated.

void render ([bool $isIframe = FALSE])
  • bool $isIframe: Indicate we're responding to an IFRAME
replaceContents (line 543)

Replace an element of the DOM tree by a given HTML code

void replaceContents (string $id, string $code, [bool $evalScripts = FALSE])
  • string $id: Element ID
  • string $code: Replace code
  • bool $evalScripts: If scripts must be evaluated
resetForm (line 330)

Reset a form by ID

  1.  $response->resetForm('hire_form');

void resetForm (string $id)
  • string $id: Form ID
runScript (line 698)

Runs a block of JS code

  1.  $response->runScript("var div = $('my_div');");
  2.  $response->runScript("$('my_element').hide(); $('my_field').addClass('class_name');");

void runScript (string $script)
  • string $script: JS code
setAttribute (line 404)

Set one or more attributes of an element

  1.  $response->setAttribute('name''maxLength'10);
  2.  $response->setAttribute('hire_form'array(
  3.    'action' => 'process_hire.php',
  4.    'method' => 'post'
  5.  ));

void setAttribute (string $id, string|array $attr, [mixed $value = ''])
  • string $id: Element ID
  • string|array $attr: Attribute name or attributes hash
  • mixed $value: Attribute value
setCharset (line 70)

Set response charset

void setCharset (string $charset)
  • string $charset: Charset
setStyle (line 431)

Set one or more style properties of an element

  1.  $response->setStyle('error_header''background-color''red');
  2.  $response->setStyle('login_div'array(
  3.    'opacity' => 0.8,
  4.    'border' => '1px solid black'
  5.  ));

void setStyle (string $id, string|array $attr, [mixed $value = ''])
  • string $id: Element ID
  • string|array $attr: Attribute name or attributes hash
  • mixed $value: Attribute value
setValue (line 111)

Defines the value of one or more form fields

  1.  $response->setValue('name''John');
  2.  $response->setValue(array(
  3.    'address' => 'Elm Street, 123',
  4.    'phone' => '5555-6666'
  5.  ));

void setValue (string $id, [mixed $value = ''])
  • string $id: Field ID or hash array of fields/values
  • mixed $value: Field value
setValueByName (line 136)

Defines the value of a field given its name and the form ID

void setValueByName (string $formId, string $fieldName, mixed $value)
  • string $formId: Form ID
  • string $fieldName: Field name
  • mixed $value: Field value
setVariable (line 680)

Set a Javascript variable

  1.  $response->setVariable('myFlag'TRUE);
  2.  $response->setVariable('globalArray'array(123));

void setVariable (string $name, mixed $value)
  • string $name: Variable name
  • mixed $value: Variable value
show (line 347)

Shows one or more elements

  1.  $response->show(array('error_header''error_details'));
  2.  $response->show('login_div');

void show (string|array $id)
  • string|array $id: Element ID or IDs
updateContents (line 500)

Updates the contents of an element

  1.  $response->updateContents('error_div'"Error trying to save record to the database!");

void updateContents (string $id, string $code, [bool $evalScripts = FALSE], [bool $useDom = FALSE])
  • string $id: Element ID
  • string $code: Code
  • bool $evalScripts: If scripts must be evaluated
  • bool $useDom: If DOM must be used to update the contents

Inherited Methods

Inherited From PHP2Go

PHP2Go::PHP2Go()
PHP2Go::equals()
PHP2Go::generateUniqueId()
PHP2Go::getClassName()
PHP2Go::getConfigVal()
PHP2Go::getLangVal()
PHP2Go::getObjectName()
PHP2Go::getParentName()
PHP2Go::hasDestructor()
PHP2Go::hashCode()
PHP2Go::isA()
PHP2Go::isSubclassOf()
PHP2Go::logError()
PHP2Go::raiseError()
PHP2Go::registerDestructor()
PHP2Go::registerShutdownFunc()
PHP2Go::retrieve()
PHP2Go::store()
PHP2Go::__toString()

Documentation generated on Sun, 07 Oct 2007 19:20:00 -0300 by phpDocumentor 1.3.2