diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6f7556c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+This is the classic [Backbone Todos app](http://addyosmani.github.com/todomvc),
+backended by a JSON API in PHP.
diff --git a/index.html b/index.html
index 08c4b9f..266011d 100644
--- a/index.html
+++ b/index.html
@@ -21,7 +21,7 @@
@@ -39,9 +39,7 @@
Todos
diff --git a/index.php b/index.php
index d93e83c..b82332d 100644
--- a/index.php
+++ b/index.php
@@ -5,7 +5,6 @@
class Todo extends ActiveRecord\Model { }
ActiveRecord\Config::initialize(function($cfg) {
- $cfg->set_model_directory('.');
$cfg->set_connections(array('development' => 'mysql://root:@localhost/todos'));
});
@@ -13,13 +12,26 @@ class Todo extends ActiveRecord\Model { }
$app = new Slim();
$app->get('/json', function() {
- echo "Index";
+ echo json_encode(array_map(function($todo) { return $todo->attributes(); }, Todo::all()));
});
+
+$app->post('/json', function() {
+ global $app;
+ $todo = new Todo(json_decode($app->request()->getBody(), true));
+ $todo->save();
+});
+
$app->get('/json/:id', function($id) {
- echo "Hello, $id!";
+ echo Todo::find($id)->to_json();
+});
+
+$app->put('/json/:id', function($id) {
+ global $app;
+ Todo::find($id)->update_all(array('set' => json_decode($app->request()->getBody(), true)));
});
-$app->post('/json', function() {
+$app->delete('/json/:id', function($id) {
+ Todo::find($id)->delete();
});
$app->run();
diff --git a/todos.js b/todos.js
index 3c047da..295c5e6 100644
--- a/todos.js
+++ b/todos.js
@@ -40,10 +40,10 @@ $(function(){
// Todo Collection
// ---------------
- // The collection of todos is backed by a remote server.
+ // The collection of todos is backed by a web service.
var TodoList = Backbone.Collection.extend({
- url: '/todos',
+ url: '/json',
// Reference to this collection's model.
model: Todo,
diff --git a/todos.sql b/todos.sql
index d4e2ae8..b58eb73 100644
--- a/todos.sql
+++ b/todos.sql
@@ -1,6 +1,6 @@
create table todos(
id int not null primary key auto_increment,
content varchar(50),
- done varchar(50),
- order varchar(50)
+ done tinyint(1),
+ order int
);