Actually, it is quite easy to do AJAX with Habari.

First, write your JavaScript and include it using a hook in the theme.  
For examples of this, just check out the -extras repository.

Have the AJAX request sent to example.com/habari/ajax/foobar. (You can  
generate this URL with URL::get('ajax', array('context' => 'foobar');)

Then, in your function, just implement the ajax_foobar action. (Make a  
method named action_ajax_foobar).

Whatever you do in that function (and only what you do in that  
function) will be called when the request is sent. No hacking needed,  
everything can be contained in your plugin file.

On Nov 10, 2008, at 11:38 AM, kanian wrote:

>
> I wrote a mail "plugin" based on swiftmailer and using ajax as a mean
> for sending user interactions back to the server. I followed the
> discussion about the mail functionality  to be added to habari and
> habari's design philosophy. So here I am not trying to add a mail
> functionality to Habari, nor presenting a release version of my plugin
> per se, but rather I am experimenting with ajax and Habari.
> That being, being new to Habari I had to ack the index.php code to be
> able to make my asychronous calls to Habari, since I don't know yet
> the "right" way to do ajax with Habari.
> The technique I used allows me to load only the plugin concerned with
> the request and return results from that plugin directly in the
> response.
>
> Here's how I proceded:
> In entry.single.php I added at the relevant place this piece of code:
> <?php if ( Plugins::is_loaded( 'Swiftmailer' ) ): ?>
>       <span class="post-mail-friend">
>       <?php $theme->show_mailer_prompt();?>
>       </span>
> <?php endif; ?>
>
> This is inserted by the theming engine as soon as a post is rendered
> to the screen.
> The theme_show_mailer_prompt( ) method in my plugin just returns a
> template that I named 'swiftmailer', whose most relevant part to this
> discussion is  :
>
> &nbsp; email post to a friend?
> <script language="javascript">
> function show_form_callback(responseText,statusCode,xhr)
> {
>         //some code here
>
>               $('#send_email').livequery(
>                       'click',
>                       function(){$("<div>").load("/habari/",
> {plugin:'swiftmailer',action:"mail"},send_email_callback)}
>               );
> }
>
> function show_form()
> {
>       $("<div>").load("/habari/",
> {plugin:'swiftmailer',action:"show_mail_form"},show_form_callback);
> }
>
>    //some code here
>
> </script>
>
> So I send a 'plugin' token with the request specifying which plugin
> should receive this request, and action to perform by the plugin.
> In index.php I act based on the value of that token. The relevant code
> in index.php is :
>
> header( 'Content-Type: text/html;charset=utf-8' );
>
>
> //used for ajax calls to specific plugins
> if(array_key_exists('plugin',$_POST))
> {
>
>       $plugins = Plugins::list_active();
>       foreach($plugins as $plugin)
>       {
>               $plugin_base = basename($plugin);
>               if($_POST['plugin'].'.plugin.php'==$plugin_base)
>               {
>                       include_once($plugin);
>                       Plugins::load($plugin);
>                       Plugins::act('plugins_loaded');
>                       // Start the session.
>                       Session::init();
>                       Plugins::act('init');
>                       Plugins::act($_POST['action']);
>                       exit();
>               }
>       }
>
>       echo($user_plugins.$_POST['plugin'].$ds.
> $_POST['plugin'].'.plugin.php');
>                exit();
>
> }
>
> Then in my plugin (that I called unsurprisingly
> swiftmailer.plugin.php) , I have methods like this one to perform the
> action I want:
>
>       public function action_show_mail_form( )
>       {
>               $this->theme_load();
>               echo $this->theme->fetch('swiftmailer_form' );
>       }
>
> with the theme_load function given here for clarity:
>       function theme_load()
>       {
>               $themes = new Themes();
>               $this->theme = $themes->create();
>                }
>
> Now what I would like to how many Habari design principles I have
> violated here :))
> Moer seriously how to do this properly with Habari?
>
> >


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/habari-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to