यहां बताया गया है कि हमने Google ऐप इंजन पर अजाक्स को कैसे लागू किया है, लेकिन विचार को अन्य प्लेटफ़ॉर्म पर सामान्यीकृत किया जा सकता है।
हमारे पास अजाक्स अनुरोधों के लिए एक हैंडलर स्क्रिप्ट है जो - जेएसओएन प्रतिक्रियाओं के साथ-साथ प्रतिक्रिया देती है। ,
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
ग्राहक पक्ष पर हम:
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.
आदेशों आदेश पैटर्न का एक सरल कार्यान्वयन कर रहे हैं: संरचना कुछ इस तरह (यह एक मानक GAE हैंडलर स्क्रिप्ट से एक अंश है) लग रहा है एक अजाक्स प्रतिनिधि बनाओ। Prototype.js लिखना और समझना आसान बनाता है। यहाँ एक अंश है:
new AjaxApi(resultHandlerFunction, faultHandlerFunction).getFriends()
मुझे आशा है कि इस मदद करता है:
/**
* 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
});
}
});
प्रतिनिधि कॉल करने के लिए, आप की तरह कुछ करते हैं!
http://code.google.com/webtoolkit/doc/latest/tutorial/appengine.html <- उपर्युक्त ट्यूटोरियल का नवीनतम स्थान। इच्छा है कि मैं इसे संपादित करने के लिए प्रतिनिधि था। ;) – Drew
@Drew - उत्तर में यूआरएल अपडेट किया है। सर उठाने के लिए धन्यवाद। –
धन्यवाद डेव। आह, मुझे लगता है कि यह Google वेब टूलकिट का उपयोग GAE के लिए AJAX ढांचे के रूप में करने की क्षमता से बाहर है। बहुत बुरा! क्या कोई अन्य ढांचा है? – Graviton