Any good AJAX framework for Google App Engine apps

2019-03-09 11:55发布

I am trying to implement AJAX in my Google App Engine application, and so I am looking for a good AJAX framework that will help me. Anyone has any idea?

I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?

12条回答
Anthone
2楼-- · 2019-03-09 12:15

Here is how we've implemented Ajax on the Google App Engine, but the idea can be generalized to other platforms.

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):

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.

The commands are a simple implementation of the command pattern:

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

On the client side, we create an Ajax delegate. Prototype.js makes this easy to write and understand. Here is an excerpt:

/** 
 * 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
        });    
    }

});

to call the delegate, you do something like:

new AjaxApi(resultHandlerFunction, faultHandlerFunction).getFriends()

I hope this helps!

查看更多
姐就是有狂的资本
3楼-- · 2019-03-09 12:16

I'm currently using JQuery for my GAE app and it works beautifully for me. I have a chart (google charts) that is dynamic and uses an Ajax call to grab a JSON string. It really seems to work fine for me.

查看更多
一夜七次
4楼-- · 2019-03-09 12:23

In my blog I deal with an easy way to do this - the link is: AJAX with Google App Engine. I include all the javascript and python code I used.

查看更多
闹够了就滚
5楼-- · 2019-03-09 12:23

try also GQuery for GWT. This is Java code:

public void onModuleLoad() { 
    $("div").css("color", "red").click(new Function() { 
        public void f(Element e) { 
            Window.alert("Hello"); 
            $(e).as(Effects).fadeOut(); 
        } 
    }); 
} 

Being Java code resulting in somewhat expensive compile-time (Java->JavaScript) optimizations and easier refactoring.

Nice, it isn't?

查看更多
虎瘦雄心在
6楼-- · 2019-03-09 12:24

Google has recently announced the Java version of Google App Engine. This release also provides an Eclipse plugin that makes developing GAE applications with GWT easier.

See details here: http://code.google.com/appengine/docs/java/overview.html

Of course, it would require you to rewrite your application in Java instead of python, but as someone who's worked with GWT, let me tell you, the advantages of using a modern IDE on your AJAX codebase are totally worth it.

查看更多
Juvenile、少年°
7楼-- · 2019-03-09 12:26

If you want to be able to invoke method calls from JavaScript to Python, JSON-RPC works well with Google App Engine. See Google's article, "Using AJAX to Enable Client RPC Requests", for details.

查看更多
登录 后发表回答