Blog

AJAX was never easier!

Ajax should be as easy as just write a method in our server-side code and consume it from our client javascript. No more steps!
Well, let’s illustrate it with an example:
Imagine that we have created the following method (a method to log messages):


<?php

class MyEventHandler {

  public function logMessage($message) {
      __ApplicationContext::getInstance()
                          ->getLogger()
                          ->info($message);
  }

}

And we pretend to log messages generated from our client javascript.
According to our intro, it should be as easy as just call the logMessage method from our javascript code:

<script language="javascript">

  logMessage("Message to be logged from the server side");

</script>

In the other hand, we should be able to selectively define which methods are allowed to be consumed from the client (otherwise, imagine what a security issue).
In that sense, we could let the framework know that a method is callable from javascript by just adding a comment-based notation to them:
Let see it in terms of code:

<?php

class MyEventHandler {

  /**
   * @lion remoteService
   */
  public function logMessage($message) {
      __ApplicationContext::getInstance()
                          ->getLogger()
                          ->info($message);
  }

  public function myNonCallableMethod() {
      //whatever php code here
  }

}

With the remoteService annotation we are letting lion framework know that the method foo is callable from javascript code. The framework will perform the rest of the work for us.

As soon as AJAX is asynchronous by definition, a good question could be: what about values returned by server-side methods? How are them handled by the client javascript code?
I’ll explain it in my next post (maybe tomorrow) :)
Regards Antonio

Leave a Reply