KEMBAR78
symfony & jQuery (phpDay) | PPT
symfony & jQuery ties and separations Massimiliano Arione May, 14th 2011
About me 2001 PHP 2004 PEAR 2007 symfony 2009 agile
symfony
jQuery
MVC
CPB
B ehavior progressive enhancement AJAX
PROGRESSIVE ENHANCEMENT: THE WRONG WAY sfFormExtraPlugin : sfWidgetFormJQueryDate sfWidgetFormJQueryAutocompleter sfWidgetFormTextareaTinyMCE
PROGRESSIVE   ENHACEMENT: THE GOOD WAY Just use  plain  Javascript!
AJAX: THE 4 STEPS Javascript catches an interaction with user, or with other browser events  an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server  Javascript decodes data from file and interacts with page
AJAX: THE 4 STEPS Javascript catches an interaction with user, or with other browser events  an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server  Javascript decodes data from file and interacts with page
AJAX: THE 4 STEPS Javascript catches an interaction with user, or with other browser events  an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server  Javascript decodes data from file and interacts with page
AJAX: THE 4 STEPS Javascript catches an interaction with user, or with other browser events  an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server  Javascript decodes data from file and interacts with page
AJAX: THE WRONG WAY sf 1.0: Javascript helper  sf 1.4: sfJqueryPlugin <?php echo   link_to_remote ('...'); echo   jq_link_to_remote ('...');
AJAX: THE GOOD WAY code as if Javascript wouldn't exist write your jQuery functions in the big  $().ready()  function do little adaptions to your controller write another view (tipically a JSON one)
AJAX: THE GOOD WAY code as if Javascript wouldn't exist write your jQuery functions in the big  $().ready()  function do little adaptions to your controller write another view (tipically a JSON one)
AJAX: THE GOOD WAY code as if Javascript wouldn't exist write your jQuery functions in the big  $().ready()  function do little adaptions to your controller write another view (tipically a JSON one)
AJAX: THE GOOD WAY code as if Javascript wouldn't exist write your jQuery functions in the big  $().ready()  function do little adaptions to your controller write another view (tipically a JSON one)
IN PRACTICE: LINK <?php // in the view   echo   link_to (' + ', ' cart_increase ', $item) $(' div#cart a.increase ').click(ajaxIncrease);
var  ajaxIncrease =  function (e) {   $.ajax({     url:     this.href + ' ?sf_format=json ',     success:  function (r) { increase(r, e.target); }   });    return false ; }; var  increase =  function (result, a) {    var  $span = $(a).parents(' li ').find(' span.qty ');    var  newqty = parseInt($span.text(),  10 ) +  1 ;   $span.empty().append(newqty);   $(' span#total ').empty();   $(' span#total ').append(result.total); };
<?php // in the view   // cartIncreaseSuccess.json.php use_helper(' Number ');   $arr = array(   ' total ' => format_currency($sf_user->getCartTotal(), ' EUR '), ); echo  json_encode($arr);
<?php // in the controller   // actions.class.php public  function  executeCartIncrease(sfWebRequest $request) {   $this->product = $this->getRoute()->getProduct();   $this->getUser()->cartIncrease($this->product);   // was $this->redirect('@homepage');       $this->redirectUnless($request->isXmlHttpRequest(), ' @homepage '); }
IN PRACTICE: FORM $(' div#filters form ').submit(ajaxFilter);
var  ajaxFilter = function(e) {    var  $form = $(this);   $.ajax({     type: ' POST ',     url:  $form.attr(' action ') + ' ?sf_format=json ',     data: $form.serializeArray(),     success: showProducts   });    return false ; };
Thanks! Massimiliano Arione @ garakkio blog.garak.it github.com/garak/sfjqec joind.in/talk/view/3034

symfony & jQuery (phpDay)

  • 1.
    symfony & jQueryties and separations Massimiliano Arione May, 14th 2011
  • 2.
    About me 2001PHP 2004 PEAR 2007 symfony 2009 agile
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
    B ehavior progressiveenhancement AJAX
  • 8.
    PROGRESSIVE ENHANCEMENT: THEWRONG WAY sfFormExtraPlugin : sfWidgetFormJQueryDate sfWidgetFormJQueryAutocompleter sfWidgetFormTextareaTinyMCE
  • 9.
    PROGRESSIVE   ENHACEMENT:THE GOOD WAY Just use plain Javascript!
  • 10.
    AJAX: THE 4STEPS Javascript catches an interaction with user, or with other browser events an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server Javascript decodes data from file and interacts with page
  • 11.
    AJAX: THE 4STEPS Javascript catches an interaction with user, or with other browser events an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server Javascript decodes data from file and interacts with page
  • 12.
    AJAX: THE 4STEPS Javascript catches an interaction with user, or with other browser events an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server Javascript decodes data from file and interacts with page
  • 13.
    AJAX: THE 4STEPS Javascript catches an interaction with user, or with other browser events an XMLHttpRequest object send a request to server, without breaking the flow an XML (or other format) is returned by server Javascript decodes data from file and interacts with page
  • 14.
    AJAX: THE WRONGWAY sf 1.0: Javascript helper sf 1.4: sfJqueryPlugin <?php echo link_to_remote ('...'); echo jq_link_to_remote ('...');
  • 15.
    AJAX: THE GOODWAY code as if Javascript wouldn't exist write your jQuery functions in the big $().ready() function do little adaptions to your controller write another view (tipically a JSON one)
  • 16.
    AJAX: THE GOODWAY code as if Javascript wouldn't exist write your jQuery functions in the big $().ready() function do little adaptions to your controller write another view (tipically a JSON one)
  • 17.
    AJAX: THE GOODWAY code as if Javascript wouldn't exist write your jQuery functions in the big $().ready() function do little adaptions to your controller write another view (tipically a JSON one)
  • 18.
    AJAX: THE GOODWAY code as if Javascript wouldn't exist write your jQuery functions in the big $().ready() function do little adaptions to your controller write another view (tipically a JSON one)
  • 19.
    IN PRACTICE: LINK<?php // in the view echo link_to (' + ', ' cart_increase ', $item) $(' div#cart a.increase ').click(ajaxIncrease);
  • 20.
    var ajaxIncrease= function (e) {   $.ajax({     url:     this.href + ' ?sf_format=json ',     success: function (r) { increase(r, e.target); }   });   return false ; }; var increase = function (result, a) {   var $span = $(a).parents(' li ').find(' span.qty ');   var newqty = parseInt($span.text(), 10 ) + 1 ;   $span.empty().append(newqty);   $(' span#total ').empty();   $(' span#total ').append(result.total); };
  • 21.
    <?php // inthe view // cartIncreaseSuccess.json.php use_helper(' Number ');   $arr = array(   ' total ' => format_currency($sf_user->getCartTotal(), ' EUR '), ); echo json_encode($arr);
  • 22.
    <?php // inthe controller // actions.class.php public function executeCartIncrease(sfWebRequest $request) {   $this->product = $this->getRoute()->getProduct();   $this->getUser()->cartIncrease($this->product);   // was $this->redirect('@homepage');     $this->redirectUnless($request->isXmlHttpRequest(), ' @homepage '); }
  • 23.
    IN PRACTICE: FORM$(' div#filters form ').submit(ajaxFilter);
  • 24.
    var ajaxFilter= function(e) {   var $form = $(this);   $.ajax({     type: ' POST ',     url:  $form.attr(' action ') + ' ?sf_format=json ',     data: $form.serializeArray(),     success: showProducts   });   return false ; };
  • 25.
    Thanks! Massimiliano Arione@ garakkio blog.garak.it github.com/garak/sfjqec joind.in/talk/view/3034

Editor's Notes

  • #3 a brief history of my develeper evolution, with some stages: first  a plain procedural PHP developer, then some objects with PEAR, then full OOP with symfony, last (but not least) agile methods.
  • #4 is there anyone that DOES NOT know symfony? Well, symfony is a full-stack MVC framework written in PHP. This talk uses symfony for PHP code, but the core issue is not strictly related to symfony (so, you can just apply what we&apos;ll see to any framework, or generally to any PHP code)
  • #5 is there anyone that DOES NOT know jQuery? Weel, jQuery is a Javascript library.
  • #6 Here is a schema of MVC pattern, with separation between the three layes: Model, View, Controller
  • #7 Here is instead the separation concerning web pages: Conten, Presentation, Behavior
  • #8 What is behavior? It&apos;s mainly two things: progressive enhancement, and AJAX
  • #9 So, the progressive enhancement has been wrongly implemented in the past, with symfony. Those widgets, from sfFormExtraPlugin, do it the wrong way, by mixing php and javascript
  • #10 the right way is dead simple: just use plain javascript. Attach &amp;quot;ready&amp;quot; event and enhance.
  • #11 Here come the most interesting part: the famous AJAX. Let&apos;s see what AJAX is, in 4 simple steps.
  • #15 Like for behavior, also AJAX has been implemented in wrong ways in the past, with symfony. Helpers to do AJAX calls: again, this leads to php/js mixing (and to disregard separation between C and B)
  • #16 We can implement correctly AJAX in 4 steps.
  • #20 A practical example: a simple e-commerce site, with a link in the cart, to increase a quantity of an item in the cart itself.
  • #21 These two functions deals with AJAX request and with DOM manipulation after succeeded AJAX call.
  • #22 The added view is simply a JSON file, with needed data
  • #23 The only modify needed in the controller is not redirecting when request is coming from AJAX
  • #24 A similar approach is applied to forms
  • #25 This is just like the link example seen above, just with different point of tie (the form&apos;s &amp;quot;action&amp;quot; instead of link&apos;s &amp;quot;href&amp;quot;)
  • #26 That&apos;s it! You can find a complete symfony project on a github repository