Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how we've implemented Ajax on the Google App Engine, but the idea can be generalized to other platforms.</p> <p>We have a handler script for Ajax requests that responds -mostly- with JSON responses. The structure looks something like this (this is an excerpt from a standard GAE handler script):</p> <pre><code>def Get(self, user): self.handleRequest() def Post(self, user): self.handleRequest() def handleRequest(self): ''' A dictionary that maps an operation name to a command. aka: a dispatcher map. ''' operationMap = {'getfriends': [GetFriendsCommand], 'requestfriend': [RequestFriendCommand, [self.request.get('id')]], 'confirmfriend': [ConfirmFriendCommand, [self.request.get('id')]], 'ignorefriendrequest': [IgnoreFriendRequestCommand, [self.request.get('id')]], 'deletefriend': [DeleteFriendCommand, [self.request.get('id')]]} # Delegate the request to the matching command class here. </code></pre> <p>The commands are a simple implementation of the command pattern:</p> <pre><code>class Command(): """ A simple command pattern. """ _valid = False def validate(self): """ Validates input. Sanitize user input here. """ self._valid = True def _do_execute(self): """ Executes the command. Override this in subclasses. """ pass @property def valid(self): return self._valid def execute(self): """ Override _do_execute rather than this. """ try: self.validate() except: raise return self._do_execute() # Make it easy to invoke commands: # So command() is equivalent to command.execute() __call__ = execute </code></pre> <p>On the client side, we create an Ajax delegate. Prototype.js makes this easy to write and understand. Here is an excerpt:</p> <pre><code>/** * Ajax API * * You should create a new instance for every call. */ var AjaxAPI = Class.create({ /* Service URL */ url: HOME_PATH+"ajax/", /* Function to call on results */ resultCallback: null, /* Function to call on faults. Implementation not shown */ faultCallback: null, /* Constructor/Initializer */ initialize: function(resultCallback, faultCallback){ this.resultCallback = resultCallback; this.faultCallback = faultCallback; }, requestFriend: function(friendId){ return new Ajax.Request(this.url + '?op=requestFriend', {method: 'post', parameters: {'id': friendId}, onComplete: this.resultCallback }); }, getFriends: function(){ return new Ajax.Request(this.url + '?op=getfriends', {method: 'get', onComplete: this.resultCallback }); } }); </code></pre> <p>to call the delegate, you do something like:</p> <pre><code>new AjaxApi(resultHandlerFunction, faultHandlerFunction).getFriends() </code></pre> <p>I hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload