Open-Source Php5 Mvc Framework

ADVERTISEMENT

Open-Source PHP5 MVC Framework
Agile Development
Helpers
PART 1 - JAVASCRIPT(JS) and AJAX (remote calls)
<?php echo use_helper('Javascript') ?>
JAVASCRIPT AND AJAX HELPERS
REMOTE CALL PARAMETERS
All the AJAX helpers can take other parameters, in addition to
JavaScript Helpers
the update and url parameters:
link_to_function ($name, $function, $html_options=array())
position
<?php echo
("Click me!", "alert('foobar')") ?> // will generate:
link_to_function
The position parameter can be defined as:
<a href="#" onClick="alert('foobar');return none;">Click me!</a>
Value
Position
javascript_tag ($content)
before
before the element
after
after the element
<?php echo
("document.getElementById('indicator').innerHTML=
javascript_tag
top
at the top of the content of element
'<strong>Data processing complete</strong>';") ?>
bottom
bottom of the content of element
update_element_function ($element_id, $options=array())
conditions
<?php echo
(
( ‘indicator', array(
javascript_tag update_element_function
"position"=>"after", "content" =>"<strong>Data processing complete</strong>" )))?>
confirm
'confirm' => 'Are you sure?’
Ajax Helpers
A JS dialog box showing 'Are you sure?' will pop-up when
the user clicks on the caller, and the module/action will be
An AJAX interaction is made up of three parts:
called only if the user confirms his choice by clicking 'Ok'.
* a caller (a link, button or any control that the user manipulates to launch the action)
* a server action
condition
* a zone in the page to display the result of the action to the user.
'condition' => "$('elementID') == true",
Symfony provides multiple helpers to insert AJAX interaction in your templates by
The remote call can also be conditioned by a test
putting the caller in a link, a button, a form, or a clock. These helpers output HTML
performed on the browser side (in JavaScript).
code, not JavaScript.
link_to_remote ($name, $options=array(), $html_options=array())
script execution
'script' => true
<?php echo
('Delete this post', array(
link_to_remote
If the response code of the AJAX call (the code sent by
'update' => 'indicator', 'url' => 'post/delete?id='.$post->getId() )) ?>
the server, inserted in the update element) contains JS,
these scripts are not executed by default. This is to
remote_function ($options=array())
prevent remote attack risks.The ability to execute scripts
<?php echo
(
(array(
javascript_tag remote_function
change part of the
in remote responses explicitly with the script option.
'update' => 'myzone',
page according to
'url'
=> 'mymodule/myaction'
a server response
callbacks
))) ?>
Event
Callback
before
Before request is initiated
form_remote_tag ($options=array(), $options_html=array())
after
Immediately after request is initiated and
<?php echo
(array(
form_remote_tag
before loading
'update' => 'item_list', 'url' => '@item_add' )) ?>
opens a <form>, just like
When the remote response is being loaded
loading
<label for="item">Item:</label>
the form_tag() helper
When the browser has finished loading the
loaded
does.
<?php echo input_tag('item') ?>
remote response
<?php echo submit_tag('Add') ?>
interactive
When the user can interact with the remote
</form>
response, even though it has not finished loading
When the XMLHttpRequest is completed, and
success
observe_field ($field_id, $options=array())
the HTTP status code is in the 2XX range
<?php echo form_tag('@item_add_regular') ?>
When the XMLHttpRequest is completed, and
the module/action written in the
failure
<label for="item">Item:</label>
@item_being_typed rule will be
the HTTP status code is not in the 2XX range
<?php echo input_tag('item') ?>
called each time the item field
404
When the request returns a 404 status
changes, and the action will be
<?php echo submit_tag('Add') ?>
complete
When the XMLHttpRequest is complete
able to get the current item value
(fires after success or failure, if present)
<?php echo
('item', array(
observe_field
from the value request parameter.
'update' => 'item_suggestion',
e.g.:
<?php echo link_to_remote('Delete this post', array(
If you want to pass something else
'url'
=> '@item_being_typed'
'update'
=> 'indicator',
than the value of the observed field,
)) ?>
'url'
=> 'post/delete?id='.$post->getId(),
you can specify it as a JavaScript
'position' => 'after',
expression in the ‘with’ parameter
</form>
‘confirm' => 'Are you sure?',
periodically_call_remote ($options=array())
'script'
=> true
'loading'
=> "Element.show('indicator')",
<?php echo
(array(
periodically_call_remote
this helper is an AJAX
'complete' => "Element.hide('indicator')" )) ?>
'frequency' => 60,
interaction triggered every x
'update'
=> 'notification',
seconds. It is not attached to a
HTML control, but runs
'url'
=> '@watch',
NOTES
transparently in the background,
'with'
=> "'param=' + $('mycontent').value"
as a behaviour of the whole page
* Actions called as remote functions know that they are in
)) ?>
an AJAX transaction, and therefore automatically don't
Another functions
include the web debug toolbar in development. Also, they
submit_to_remote ($name, $value, $options=array())
skip the decoration process (their template is not included
evaluate_remote_response ()
in a layout by default). If you want an Ajax view to be
observe_form ($form_id, $options=array())
decorated, you need to specify explicitly has_layout: true
visual_effect ($name, $element_id=false, $js_options=array())
for this view in the module view.yml file. Actions called
sortable_element ($element_id, $options=array())
through Ajax, return true to the following call:
draggable_element ($element_id, $options=array())
$isAjax = $this->isXmlHttpRequest();
drop_receiving_element ($element_id, $options=array())
javascript_cdata_section ($content)
* The AJAX helpers won't work if the URL of the remote
input_auto_complete_tag ($name, $value, $url, $tag_options=array(),
action doesn't belong to the same domain as the current
$completion_options=array())
page. This restriction exists for security reasons, and relies
input_in_place_editor_tag ($name, $url, $editor_options=array())
on browsers limitations that cannot be bypassed.
The
Prototype
and
Script.aculo.us
JavaScript libraries are bundled with the symfony.
This cheat-sheet is not an official part of the symfony documentation

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go