symfony & jquery (pug)

19
symfony & jQuery ties and separations Massimiliano Arione April, 26th 2011

Upload: massimiliano-arione

Post on 29-Nov-2014

1.749 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: symfony & jQuery (PUG)

symfony & jQueryties and separations

Massimiliano Arione

April, 26th 2011

Page 2: symfony & jQuery (PUG)

About me

• 2001: PHP developer• 2006: GrUSP member• 2009: member of GrUSP steering committee • 2010: PUG Rome president :-)

Page 3: symfony & jQuery (PUG)

symfony

Page 4: symfony & jQuery (PUG)

jQuery

Page 5: symfony & jQuery (PUG)

MVC

Page 6: symfony & jQuery (PUG)

CPB

Page 7: symfony & jQuery (PUG)

oprogressive enhancementoAJAX

Page 8: symfony & jQuery (PUG)

PROGRESSIVE ENHANCEMENT:THE WRONG WAY

sfFormExtraPlugin:• sfWidgetFormJQueryDate• sfWidgetFormJQueryAutocompleter• sfWidgetFormTextareaTinyMCE

Page 9: symfony & jQuery (PUG)

PROGRESSIVE ENHACEMENT: THE GOOD WAY

Just use plain javascript!

Page 10: symfony & jQuery (PUG)

AJAX: THE 4 STEPS

1. Javascript catches an interaction with user, or with other browser events

2. an XMLHttpRequest object send a request to server, without breaking the flow

3. an XML (or other format) is returned by server 4. Javascript decodes data from file and interacts

with page

Page 11: symfony & jQuery (PUG)

AJAX: THE WRONG WAY

1.0 Javascript helper 1.4 sfJqueryPlugin

Page 12: symfony & jQuery (PUG)

AJAX: THE GOOD WAY

1.code as if Javascript wouldn't exist2.write your jQuery functions in the big $().ready() function

3.do little adaptions to your controller4.write another view (tipically a JSON

one)

Page 13: symfony & jQuery (PUG)

IN PRACTICE: LINK

<?phpecho link_to('+', 'cart_increase', $item)

$('div#cart a.increase').click(ajaxIncrease);

Page 14: symfony & jQuery (PUG)

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 $li = $(a).parents('li');  var $span = $li.find('span.quantity');  var oldq = parseInt($span.text(), 10);  var newq = oldq + 1;  $span.empty().append(newq);  $('span#total').empty();  $('span#total').append(result.total);};

Page 15: symfony & jQuery (PUG)

<?php// cartIncreaseSuccess.json.phpuse_helper('Number'); $arr = array(  'total' => format_currency($sf_user->getCartTotal(), 'EUR'),);

echo json_encode($arr);

Page 16: symfony & jQuery (PUG)

<?php// actions.class.phppublic function executeCartIncrease(sfWebRequest $request){  $this->product = $this->getRoute()->getProduct();  $this->getUser()->cartIncrease($this->product);  // was $this->redirect('@homepage');    $this->redirectUnless($request->isXmlHttpRequest(), '@homepage');}

Page 17: symfony & jQuery (PUG)

IN PRACTICE: FORM

$('div#filters form').submit(ajaxFilter);

Page 18: symfony & jQuery (PUG)

var ajaxFilter = function(e) {  var $form = $(this);  $.ajax({    type:    'POST',    url:     $form.attr('action') + '?sf_format=json',    data:    $form.serializeArray(),    success: showProducts  });  return false;};

Page 19: symfony & jQuery (PUG)

Thanks!

Massimiliano [email protected]

github.com/garak/sfjqec