-
Notifications
You must be signed in to change notification settings - Fork 4
Home
ccarpenterg edited this page Mar 30, 2012
·
14 revisions
![Tornado] (http://www.tornadoweb.org/static/tornado.png)
Previously I had created backends for the Backbone.js todos example using Google App Engine and Django. Now it's the time for Tornado Web Server. Tornado is not a web framework so I had to add the SQLAlchemy ORM to the stack.
The main task, again, was to develop the RESTful handler to support the Backbone.js JSON interface.
class RESTfulHandler(BaseHandler):
def get(self, id):
...
def post(self, id):
...
def put(self, id):
...
def delete(self, id):
...
I've used the same Backbone.js todos application. So I pointed the collection (TodoList) to the url /todos
.
window.TodoList = Backbone.Collection.extend({
// Reference to this collection's model.
model: Todo,
// Save all of the todo items under the `"todos"` namespace.
//localStorage: new Store("todos"),
url: '/todos',
Then I mapped the url to the RESTful handler:
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
(r"/todos\/?([0-9]*)", RESTfulHandler),
]
The regular expression "/todos\/?([0-9]*)"
match the Backbone.js RESTful interface HTTP methods and urls:
- read -> GET /todos[/id]
- create -> POST /todos
- update -> PUT /todos/id
- delete -> DELETE /todos/id
Once you've worked with the Django ORM, SQLAlchemy feels very natural.