[dev] Ajax on Horde

Luis Felipe Marzagao lfbm.andamentos at gmail.com
Wed Oct 11 23:15:25 UTC 2017



Em 11/10/2017 04:33, Sebastian Birnbach escreveu:
> Not much is known about Horde's way to do Ajax. The doc on
> https://wiki.horde.org/Doc/Dev/HordeAjaxApplications seems to be a bit
> wobbly and is at least partly outdated.
>
> I seek to understand the very basic exchange of information between a
> client side document and a server side class. Looking into Horde is often
> like looking into a live organism: too many things going on at the same
> time to understand an isolated feature.
>
> On my own I came about this far:
>
> * <app> registers an Ajax handler in <app>/lib/Ajax/Application.php:
> class IMP_Ajax_Application extends Horde_Core_Ajax_Application{
>    $this->addHandler(Some_Path_To_Class);
>   }
>
> * <app>/lib/Some/Path/To/Class.php defines the processing code:
> class IMP_Ajax_Application_Handler_Draft extends
> Horde_Core_Ajax_Application_Handler{
>   public function foo() {return "hello world"}
> }
>
> * <app>/js/<app>.js defines a variable that contains a function that
> eventually calls doAction(action, params, opts)
> [this bit is unclear to me]
>
> * on the client side, based on some event, call that function so that
> doAction() gets triggered
>
> * a request will be sent to Horde Core which distributes it back to the
> registered class to process and return its response
>
> * now, the response should be passed on to some callback function in the
> <app>.js file, I suppose

Here's a working example from my private app called Cashier. Hope it helps:

cashier/js/form_transaction.js:

HordeCore.doAction(
             /* PHP Action */
             'getCategoryUnitPrice',

             /* POST parameters */
             {
                 selected_category_id: selectedCategoryId
             },

             /* Options */
             {
                 callback: function(resp) {    /* here the variable resp 
is the response returned (an object with unitPrice property) */
                     doUnitPriceUpdate(resp.unitPrice);
                     makeUnitPriceFieldBright();
                     /* Cache the result. */
                     this.cache.unitPrice[selectedCategoryId] = 
resp.unitPrice;
                     this.updateItemTotal(row);
                     this.updateRelatedFields();
                     HordeCore.loadingImg('loadingimg', baseElt, false);
                 }.bind(this),
                 ajaxopts: {
                     asynchronous: true
                 }
             }
         );


cashier/lib/Ajax/Application.php:

class Cashier_Ajax_Application extends Horde_Core_Ajax_Application
{
     /**
      * Application specific initialization tasks should be done in here.
      */
     protected function _init()
     {
$this->addHandler('Cashier_Ajax_Application_Handler_FormTransaction');
     }
}


cashier/lib/Ajax/Application/Handler/FormTransaction.php:

class Cashier_Ajax_Application_Handler_FormTransaction extends 
Horde_Core_Ajax_Application_Handler
{
     public function getCategoryUnitPrice()
     {
         $obj = new stdClass();
         $driver = $GLOBALS['injector']->getInstance('Cashier_Driver');
         $cat_obj = 
$driver->getCategoryById($this->vars->selected_category_id);
         $obj->unitPrice = 
Cashier_Util::formatMoney($cat_obj->category_unit_price);
         return $obj;
     }
}

>
> Could somebody please shed some light on this?
>
> Thanks
>
>    Sebastian



More information about the dev mailing list