25
symfony & jQuery ties and separations Massimiliano Arione May, 14th 2011

symfony & jQuery (phpDay)

Embed Size (px)

Citation preview

Page 1: symfony & jQuery (phpDay)

symfony & jQueryties and separations

Massimiliano Arione

May, 14th 2011

Page 2: symfony & jQuery (phpDay)

About me

• 2001 PHP• 2004 PEAR• 2007 symfony• 2009 agile

Page 3: symfony & jQuery (phpDay)

symfony

Page 4: symfony & jQuery (phpDay)

jQuery

Page 5: symfony & jQuery (phpDay)

MVC

Page 6: symfony & jQuery (phpDay)

CPB

Page 7: symfony & jQuery (phpDay)

Behavior

• progressive enhancement

• AJAX

Page 8: symfony & jQuery (phpDay)

PROGRESSIVE ENHANCEMENT:THE WRONG WAY

sfFormExtraPlugin:• sfWidgetFormJQueryDate• sfWidgetFormJQueryAutocomplet

er• sfWidgetFormTextareaTinyMCE

Page 9: symfony & jQuery (phpDay)

PROGRESSIVE ENHACEMENT: THE GOOD WAY

Just use plain Javascript!

Page 10: symfony & jQuery (phpDay)

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 (phpDay)

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 12: symfony & jQuery (phpDay)

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 13: symfony & jQuery (phpDay)

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 14: symfony & jQuery (phpDay)

AJAX: THE WRONG WAY

sf 1.0: Javascript helper sf 1.4: sfJqueryPlugin

<?phpecho link_to_remote('...');echo jq_link_to_remote('...');

Page 15: symfony & jQuery (phpDay)

AJAX: THE GOOD WAY

1.code as if Javascript wouldn't exist

2.write your jQuery functions in the big $().ready() function

3.do little adaptions to your controller

4.write another view (tipically a JSON one)

Page 16: symfony & jQuery (phpDay)

AJAX: THE GOOD WAY

1.code as if Javascript wouldn't exist

2.write your jQuery functions in the big $().ready() function

3.do little adaptions to your controller

4.write another view (tipically a JSON one)

Page 17: symfony & jQuery (phpDay)

AJAX: THE GOOD WAY

1.code as if Javascript wouldn't exist

2.write your jQuery functions in the big $().ready() function

3.do little adaptions to your controller

4.write another view (tipically a JSON one)

Page 18: symfony & jQuery (phpDay)

AJAX: THE GOOD WAY

1.code as if Javascript wouldn't exist

2.write your jQuery functions in the big $().ready() function

3.do little adaptions to your controller

4.write another view (tipically a JSON one)

Page 19: symfony & jQuery (phpDay)

IN PRACTICE: LINK

<?php// in the view echo link_to('+', 'cart_increase', $item)

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

Page 20: symfony & jQuery (phpDay)

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);};

Page 21: symfony & jQuery (phpDay)

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

echo json_encode($arr);

Page 22: symfony & jQuery (phpDay)

<?php// in the controller // 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 23: symfony & jQuery (phpDay)

IN PRACTICE: FORM

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

Page 24: symfony & jQuery (phpDay)

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 25: symfony & jQuery (phpDay)

Thanks!

Massimiliano [email protected]

github.com/garak/sfjqec

joind.in/talk/view/3034