View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_View_Helper_Ajax
{zone-data}

{zone-data:proposer-list}
[Truppe Steven|mailto:steven.truppe@chello.at]
{zone-data}

{zone-data:revision}
1.1 - 1 August 2006: Updated from community comments.
{zone-data}

{zone-data:overview}
Zend_View_Helper_Ajax is a simple view helper to use the javascript library of your preference to send AJAX requests.

{zone-data}

{zone-data:references}
* [Prototype library|http://www.prototypejs.org/]
* [jQuery library|http://jquery.com]
{zone-data}

{zone-data:requirements}

* This view helper *will* provide a way to use different javascript libraries to send AJAX requests.
* It *will* be able to send formular data.
* It *will* be able to send and recieve text & json & xml.
* It *will* provide global options for handling multiple AJAX request with same basic settings.

{zone-data}

{zone-data:dependencies}
* Zend_View
{zone-data}

{zone-data:operation}
{code}
/**
* Here a basic usage example, it's still in experimental phase so comments are welcome.
* (inside view)
*/
$this->ajax()
->fetch('http://xyz.com/someUrl',
array(
'method' => 'get'
), array (
'onSucess' => 'function(data) { alert("test"); }',
'onError' => 'function(error) { ... }'
));

/**
* The other way to use one optionset for multiple requests would look like this:
*/
$options = array(
'url' => 'http://someurl/',
'onSuccess' => 'function (data){ ... }',
'onError' => 'function (){ ... }'
);

$this->ajax($options)
->sendForm(form1_id)
->sendForm(form2_id)
...
->sendForm(formn_id);

/**
* To be conform with the framework there should also be the 'set' methods
*/
$this->ajax()
->setMethod('get')
->setType('ajax')
->setUrl('http:/...')
->fetch();

{code}
{zone-data}

{zone-data:milestones}
At the moment i try to find the common use cases that should get solved and i'm working on the basic implementation to get a few working examples.

* Milestone 1: "\[DONE\]" think about how to implement things
* Milestone 2: get user feedback to find basic use cases
* Milestone 3: final implementation
* Milestone 4: unit tests


{zone-data}

{zone-data:class-list}
* Zend_View_Helper_Ajax
* Zend_View_Helper_Ajax_Adapter_Interface
* Zend_View_Helper_Ajax_Adapter_Abstract
* Zend_View_Helper_Ajax_Adapter_Prototype
* Zend_View_Helper_Ajax_Adapter_Jquery
{zone-data}

{zone-data:use-cases}
||UC-01|| send Form data
||UC-02|| insert result of AJAX request into DOM element (clear-and-insert, append.top, append.bottom)
||UC-03|| pass result of AJAX request to a callback (onSuccess, onError)

Please make comments on other use cases that might be of interest.
{zone-data}

{zone-data:skeletons}
{code}
interface Zend_View_Helper_Ajax_Adapter_Interface
{
public function fetch($url, $options, $callbacks);
public function sendForm($url, $options, $callbacks);
public function onDomReady($callback);
}

/**
* To provide default values
*/
abstract class Zend_View_Helper_Ajax_Adapter_Abstract implements Zend_View_Helper_Ajax_Adapter_Interface
{
/**
* default request type
*/
protected $_type = 'text';

/**
* default HTTP request Method
*/
protected $_method = 'get';
}

/**
* The view helper
*/
class Zend_View_Helper_Ajax
{
protected $_adapter = null;

/**
* Factory Pattern to load adapter class at runtime.
*
* example (inside <head> of the view)
* ---------------------
* $this->ajax()
* ->adapterFactory('prototype', 'js/prototype.js');
*
* @param $adapter The adapter class to use
* @param $library The javascript file of the library
*/
public function adapterFactory($adapter, $library)
{
if (null !== $this->_adapter) return;

$adapterName = ucfirst(strtolower($adapter));
if (require_once 'Byteshelter/View/Helper/Ajax/Adapter/'.$adapterName.'.php')
{
$className = 'Byteshelter_View_Helper_Ajax_Adapter_'.$adapterName;
$this->_adapter = new $className;
echo '<script type="text/javascript" src="'.$library.'"></script>';
}
else {
throw new Exception('View Helper Ajax Adapter '.$classname.' not found!');
}
return $this;
}

/**
* this method is the only one of the class that is visible to the view, so i just
* return $this to have access to the other methods.
*
* Maybe later this method could also be used to set options that are used during the next
* call of $this->ajax() in a view.
*/
public function ajax()
{
return $this;
}
}

{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>