<?php

/**
 * Response renderer for AJAX requests
 *
 * @version DR1.7
 */
class ResponseAjaxBackend extends ResponseBackend {



	/**
	 * Returns HTTP response in Tigermouse Ajax callback format
	 */
	public function render(ArrayObject $stack) {
		$out = array();
		foreach ($stack as $entry) {
			list($handler, $args) = $entry;
			if ($handler == 'update') {
				$view = $args[0];
				$out[] = json_encode(array('update', $view->getId(), $view->__toString(), $view->getWrappingElement()));
			} else {
				$out[] = json_encode(array_merge(array((string)$handler), $this->argsToString($args)));
			}
		}
		return json_encode($out);
	}



	/**
	 * Implicit property accessor
	 *
	 * Returns an instance of transfer object for accessing JavaScript object
	 * methods from server side.
	 * Example: <code>$this->request->document->write('foo')</code>
	 * will run <code>document.write('foo')</code> on client side.
	 */
	public function __get($property) {
		require_once 'ResponseJsHook.class.php';
		return new ResponseJsHook($property, $this);
	}



} // class ResponseAjaxBackend

?>