14. AJAX: THE WRONG WAY sf 1.0: Javascript helper sf 1.4: sfJqueryPlugin <?php echo link_to_remote ('...'); echo jq_link_to_remote ('...');
15.
16.
17.
18.
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 // in the view // cartIncreaseSuccess.json.php use_helper(' Number '); $arr = array( ' total ' => format_currency($sf_user->getCartTotal(), ' EUR '), ); echo json_encode($arr);
22. <?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 '); }
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.
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'll see to any framework, or generally to any PHP code)
is there anyone that DOES NOT know jQuery? Weel, jQuery is a Javascript library.
Here is a schema of MVC pattern, with separation between the three layes: Model, View, Controller
Here is instead the separation concerning web pages: Conten, Presentation, Behavior
What is behavior? It's mainly two things: progressive enhancement, and AJAX
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
the right way is dead simple: just use plain javascript. Attach &quot;ready&quot; event and enhance.
Here come the most interesting part: the famous AJAX. Let's see what AJAX is, in 4 simple steps.
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)
We can implement correctly AJAX in 4 steps.
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.
These two functions deals with AJAX request and with DOM manipulation after succeeded AJAX call.
The added view is simply a JSON file, with needed data
The only modify needed in the controller is not redirecting when request is coming from AJAX
A similar approach is applied to forms
This is just like the link example seen above, just with different point of tie (the form's &quot;action&quot; instead of link's &quot;href&quot;)
That's it! You can find a complete symfony project on a github repository