python Google App Engine 应用程序有什么好的 AJAX 框架吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any good AJAX framework for Google App Engine apps?
提问by Graviton
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?
我正在尝试在我的 Google App Engine 应用程序中实现 AJAX,因此我正在寻找一个可以帮助我的优秀 AJAX 框架。任何人有任何想法?
I am thinking about Google Web Toolkit, how good it is in terms of creating AJAX for Google App Engine?
我在考虑 Google Web Toolkit,它在为 Google App Engine 创建 AJAX 方面有多好?
采纳答案by Dave Webb
As Google Web Toolkit is a subset of Javait works best when you Java at the backend too. Since Google App Engine is currently Python onlyI think you'd have to do a lot of messing about to get your server and client to talk nicely to each other.
由于 Google Web Toolkit 是Java 的一个子集,因此当您在后端也使用Java时效果最佳。由于 Google App Engine 目前仅使用 Python,我认为您必须做很多事情才能让您的服务器和客户端能够很好地相互交流。
jQuery seems to be the most popular JavaScript library option in the AJAX Tag at DjangoSnippets.com.
jQuery 似乎是DjangoSnippets.com 上 AJAX 标签中最流行的 JavaScript 库选项。
Edit:The above is only true of Google App Engine applications written in Python. As Google App Engine now supports Java, GWT could now be a good choice for writing an AJAX front end. Google even have a tutorial showing you how to do it.
编辑:以上仅适用于用 Python 编写的 Google App Engine 应用程序。由于 Google App Engine 现在支持 Java,GWT 现在可能是编写 AJAX 前端的不错选择。 谷歌甚至有一个教程向你展示如何做到这一点。
回答by Scott Kirkwood
A nice way is to use an AJAX library is to take advantage of Google's AJAX Libraries API service. This is a bit faster and cleaner than downloading the JS and putting it in your /static/
folder and doesn't eat into your disk quota.
使用 AJAX 库的一个好方法是利用Google 的 AJAX 库 API 服务。这比下载 JS 并将其放在您的/static/
文件夹中更快更干净,并且不会占用您的磁盘配额。
In your javascript you would just put, for example:
在您的 javascript 中,您只需输入,例如:
google.load("jquery", "1.3.2");
and/or
和/或
google.load(google.load("dojo", "1.3.0");
Somewhere in your header you would put something like:
在您的标题中的某处,您会放置以下内容:
<script src="http://www.google.com/jsapi?key=your-key-here"></script>
And that's all you need to use Google's API libraries.
这就是您使用 Google 的 API 库所需的全部内容。
回答by Christian Berg
There is no reason why you shouldn't use GAE and Google Web Toolkit (GWT) together. You write your backend code in Python and the frontend code in Java (and possibly some JavaScript), which is then compiled to JavaScript. When using another AJAX framework you will also have this difference between server and client side language.
没有理由不应该将 GAE 和 Google Web Toolkit (GWT) 一起使用。您使用 Python 编写后端代码,使用 Java(可能还有一些 JavaScript)编写前端代码,然后将其编译为 JavaScript。当使用另一个 AJAX 框架时,服务器端语言和客户端语言之间也会有这种差异。
GWT has features that make remote invocation of java code on the server easier, but these are entirely optional. You can just use JSON or XML interfaces, just like with other AJAX frameworks.
GWT 具有使远程调用服务器上的 Java 代码更容易的功能,但这些功能完全是可选的。您可以只使用 JSON 或 XML 接口,就像使用其他 AJAX 框架一样。
GWT 1.5 also comes with JavaScript Overlay Types, that basically allow you to treat a piece of JSON data like a Java object when developing the client side code. You can read more about this here.
GWT 1.5 还带有 JavaScript Overlay Types,它基本上允许您在开发客户端代码时将一段 JSON 数据视为 Java 对象。您可以在此处阅读更多相关信息。
Update:
更新:
Now that Google has added Java support for Google App Engine, you can develop both backend and frontend code in Java on a full Google stack - if you like. There is a nice Eclipse pluginfrom Google that makes it very easy to develop and deploy applications that use GAE, GWT or both.
既然 Google 已经为 Google App Engine 添加了 Java 支持,您可以在完整的 Google 堆栈上用 Java 开发后端和前端代码 - 如果您愿意。Google 提供了一个不错的Eclipse 插件,它可以非常轻松地开发和部署使用 GAE、GWT 或两者的应用程序。
回答by mahmoud
Here is how we've implemented Ajax on the Google App Engine, but the idea can be generalized to other platforms.
下面是我们在 Google App Engine 上实现 Ajax 的方式,但这个想法可以推广到其他平台。
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):
我们有一个用于 Ajax 请求的处理程序脚本,该脚本主要使用 JSON 响应进行响应。结构看起来像这样(这是标准 GAE 处理程序脚本的摘录):
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 委托。Prototype.js 使这易于编写和理解。这是摘录:
/**
* 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!
我希望这有帮助!
回答by Tim Howland
I'd recommend looking into a pure javascript framework (probably Jquery) for your client-side code, and write JSON services in python- that seems to be the easiest / bestest way to go.
我建议为您的客户端代码查看纯 javascript 框架(可能是 Jquery),并在 python 中编写 JSON 服务 - 这似乎是最简单/最好的方法。
Google Web Toolkit lets you write the UI in Java and compile it to javascript. As Dave says, it may be a better choice where the backend is in Java, as it has nice RPC hooks for that case.
Google Web Toolkit 可让您用 Java 编写 UI 并将其编译为 javascript。正如 Dave 所说,后端在 Java 中可能是更好的选择,因为在这种情况下它有很好的 RPC 钩子。
回答by Tim Howland
You may want to have a look at Pyjamas (http://pyjs.org/), which is "GWT for Python".
您可能想看看 Pyjamas ( http://pyjs.org/),它是“Python 的 GWT”。
回答by dfa
try also GQuery for GWT. This is Java code:
也试试 GWT 的 GQuery。这是Java代码:
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.
成为 Java 代码会导致编译时(Java->JavaScript)优化和更容易重构有些昂贵。
Nice, it isn't?
不错,不是吗?
回答by Christian Oudard
jQuery is a fine library, but also check out the Prototype JavaScript framework. It really turns JavaScript from being an occasionally awkward language into a beautiful and elegant language.
jQuery 是一个很好的库,但也可以查看Prototype JavaScript 框架。它确实将 JavaScript 从偶尔笨拙的语言变成了一种美丽而优雅的语言。
回答by Steve
If you want to be able to invoke method calls from JavaScript to Python, JSON-RPCworks well with Google App Engine. See Google's article, "Using AJAX to Enable Client RPC Requests", for details.
如果您希望能够调用从 JavaScript 到 Python 的方法调用,JSON-RPC可以很好地与 Google App Engine 配合使用。有关详细信息,请参阅 Google 的文章“使用 AJAX 启用客户端 RPC 请求”。
回答by o2bjang
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.
我目前正在将 JQuery 用于我的 GAE 应用程序,它对我来说效果很好。我有一个动态图表(谷歌图表),它使用 Ajax 调用来获取 JSON 字符串。这对我来说似乎真的很好。