Permalink
Browse files

works

  • Loading branch information...
1 parent 072c088 commit a634e3d018794e8a6c1b817069cbadc16c26d304 Joshua Stauter committed Apr 15, 2012
Showing with 24 additions and 12 deletions.
  1. +2 −0 README.md
  2. +2 −4 index.html
  3. +16 −4 index.php
  4. +2 −2 todos.js
  5. +2 −2 todos.sql
View
2 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.
View
6 index.html
@@ -21,7 +21,7 @@
<div id="create-todo">
<input id="new-todo" placeholder="What needs to be done?" type="text">
- <span class="ui-tooltip-top" style="display:none;">Press Enter to save this task</span>
+ <span class="ui-tooltip-top" style="display:none">Press Enter to save this task</span>
</div>
<div id="todos">
@@ -39,9 +39,7 @@
<div id="credits">
Tip o' the hat to:<br>
<a href="http://jgn.me">J&eacute;r&ocirc;me Gravel-Niquet</a>,
- <a href="http://addyosmani.com">Addy Osmani</a>,
- <a href="http://ashkenas.com">Jeremy Ashkenas</a>,
- <a href="http://tinyclouds.org">Ryan Dahl</a>,<br>
+ <a href="http://addyosmani.com">Addy Osmani</a>,<br>
and everyone else that powers the tubes.
</div>
View
20 index.php
@@ -5,21 +5,33 @@
class Todo extends ActiveRecord\Model { }
ActiveRecord\Config::initialize(function($cfg) {
- $cfg->set_model_directory('.');
$cfg->set_connections(array('development' => 'mysql://root:@localhost/todos'));
});
$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();
View
4 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,
View
4 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
);

0 comments on commit a634e3d

Please sign in to comment.