Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Performing AJAX
  • Hi, I'm new to fuel, loving what I've found so far though! This question is general, so code may not be necessary. My whole web application is going to be AJAX'd, with jQuery animations either side, it goes like this: 1) .fadeOut() a div
    2) .hide() div
    3) Perform ajax call returning data into DIV
    4) .fadeIn() div I know how to do this with a plain JS file. However, I want to use a controller to handle all this that extends Controller_Rest. I may be getting mixed up with things, but how would I go about correctly performing the animations in jquery and THEN doing the AJAX call using Controller_Rest, then returning to jquery. In my old system I did this completely with JavaScript in one big js function - so how do I do this correctly implementing controllers, models etc in fuel? Oh, here's the function I used completely in JS - I want to do the same thing using Fuel and Controller_Rest now.
    function request(div,parameters,parse) { 
      $.post('model.php', parameters,
      function(response) {
       // In this order, first fadeout div
       $(div).fadeOut(200, function() {
        // Then get the response back HIDDEN
        $(div).html(response).hide(function() {
         // Do parsing here
         // Only after everything's parsed do we fade back in
         $(div).fadeIn(200, function() {}); 
        });
       });
      });
     }
    
    Thanks EDIT: If it's not clear what I'm after, I'd appreciate first a numbered list stating what I need to do and what way I should approach it? Thanks again :)
  • If your plan is to "GET" HTML, you are not really required to use Controller_Rest, as the normal or template controllers already return HTML. If you want other formats, like json, Controller_Rest is the easiest. If you want you use Controller_Rest to return html, use the ".html" extension on the URL or set the desired format in the controller, otherwise xml will be returned. As to calling the controller, that follows the same rules as for normal controllers. Calling the REST controller with jQuery is documented here: http://api.jquery.com/jQuery.ajax/
  • Thanks for the reply HARRO, Well one part will be returning static html (say a table) and another part will need to connect to the DB and retrieve values to populate that table with. So what I'm trying to understand is how do I tie these two together correctly, in what order etc - while still maintaining the code separation that MVC provides.
  • What happens in the controller is transparent for your frontend, so whether you return static data or do a lot of processing isn't really relevant for your ajax call. From the application point of view I tend to create them entirely from modules, to make it as reusable as possible. I hardly ever have code in app/classes, other then perhaps some helper classes or base classes. In my modules' classes/controller folder I create a folder called api, in which all my controllers are that are called via REST calls. So you get ajax calls to URI's like 'http://my.web.site/users/api/controller/method/parm/parm'. The module will also contain the models and migrations belonging to it, and any admin controllers it needs. I have modified the generic admin template (as generated by oil) to support this. You can find an example in our Depot project on github. I also try to avoid cross-module dependencies by using other modules' models, if I need data that is 'governed' by another module, I use an HMVC request to internally fetch that data from the other module.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion