Skip to content

Instantly share code, notes, and snippets.

@jlebensold
Created December 12, 2011 21:07
Show Gist options
  • Save jlebensold/1469109 to your computer and use it in GitHub Desktop.
Save jlebensold/1469109 to your computer and use it in GitHub Desktop.
Deleting with REST, SLIM and JSON
<html>
<head>
<title>Names Hello</title>
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script>
<style>
*
{
margin: 0 0;
padding: 0 0;
font-family: helvetica, arial, sans-serif;
}
body {
background: #CCC;
}
#container
{
padding: 20px;
margin: 0 auto;
width: 600px;
background: #FFF;
}
#names
{
width:150px;
}
.delete
{
margin-top:4px;
font-size: 9px;
float: right;
}
</style>
<script>
$(function()
{
$("#names").delegate('.edit','click',function(e)
{
$(this).hide()
$(this).parent().append('<input type="text" value="'+$(this).text()+'"></input><button class="save">save</button>');
});
$("#names").delegate('a.delete','click',function(e)
{
$.ajax({type: 'DELETE',
dataType: 'json',
url: '/names/'+$(this).parents('li').data('id'),
success: function(resp)
{
renderNames(resp);
}});
e.preventDefault();
});
$("#names").delegate('button.save','click',function(e)
{
$.ajax({type: 'PUT',
dataType: 'json',
data : {
name: $(this).parent().find('input').val()
},
url : '/names/'+$(this).parents('li').data('id'),
success : function(resp)
{
renderNames(resp);
}
});
e.preventDefault();
});
$('a.list').click(function(e)
{
findAll();
e.preventDefault();
});
$("form").submit(function(e)
{
$.post("/names",
{name: $("#newname").val() },
function(resp) {
findAll();
});
e.preventDefault();
});
});
function findAll()
{
$.get("/names",function(resp)
{
renderNames(resp);
},"json");
};
function renderNames(names)
{
$("#names").empty();
$.each(names,function(k,v)
{
$("#names").append('<li id="name_'+v.id+'" data-id="'+v.id+'"><a class="edit">'+v.name+'</a>'+
'<small><a href="#" class="delete">delete</a></small></li>');
});
}
</script>
</head>
<body>
<div id="container">
<h3> Names <a class="list" href="#">Load</a></h3>
<ul id="names"></ul>
<form class="" id="newnamefrm">
<input type="text" id="newname"></input><input type="submit" value="Add Name" />
</form>
</div>
</body>
</html>
<html>
<head>
<title>Names Hello</title>
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script>
<style>
*
{
margin: 0 0;
padding: 0 0;
font-family: helvetica, arial, sans-serif;
}
body {
background: #CCC;
}
#container
{
padding: 20px;
margin: 0 auto;
width: 600px;
background: #FFF;
}
#names
{
width:150px;
}
.delete
{
margin-top:4px;
font-size: 9px;
float: right;
}
</style>
<script>
$(function()
{
$("#names").delegate('.edit','click',function(e)
{
$(this).hide()
$(this).parent().append('<input type="text" value="'+$(this).text()+'"></input><button class="save">save</button>');
});
$("#names").delegate('a.delete','click',function(e)
{
$.ajax({type: 'DELETE',
dataType: 'json',
url: '/names/'+$(this).parents('li').data('id'),
success: function(resp)
{
renderNames(resp);
}});
e.preventDefault();
});
$("#names").delegate('button.save','click',function(e)
{
$.ajax({type: 'PUT',
dataType: 'json',
data : {
name: $(this).parent().find('input').val()
},
url : '/names/'+$(this).parents('li').data('id'),
success : function(resp)
{
renderNames(resp);
}
});
e.preventDefault();
});
$('a.list').click(function(e)
{
findAll();
e.preventDefault();
});
$("form").submit(function(e)
{
$.post("/names",
{name: $("#newname").val() },
function(resp) {
findAll();
});
e.preventDefault();
});
});
function findAll()
{
$.get("/names",function(resp)
{
renderNames(resp);
},"json");
};
function renderNames(names)
{
$("#names").empty();
$.each(names,function(k,v)
{
$("#names").append('<li id="name_'+v.id+'" data-id="'+v.id+'"><a class="edit">'+v.name+'</a>'+
'<small><a href="#" class="delete">delete</a></small></li>');
});
}
</script>
</head>
<body>
<div id="container">
<h3> Names <a class="list" href="#">Load</a></h3>
<ul id="names"></ul>
<form class="" id="newnamefrm">
<input type="text" id="newname"></input><input type="submit" value="Add Name" />
</form>
</div>
</body>
</html>
<?php
require '../Slim/Slim.php';
require '../Name.php';
function json($obj)
{
header('Content-Type', 'application/json');
return json_encode($obj);
}
$app = new Slim();
$app->config(array('templates.path' => '../templates'));
$app->get('/names/:id',function($id) {
echo json(Name::find($id));
});
$app->get('/names',function() {
echo json(Name::findAll());
});
$app->post('/names',function() use ($app) {
$n = new Name(null,$app->request()->post('name'));
$n->create();
echo '{status: "success" }';
});
$app->put('/names/:id',function($id) use ($app) {
$name = Name::find($id);
$name->name = $app->request()->put('name');
$name->update();
echo json(Name::findAll());
});
$app->delete('/names/:id', function($id) use ($app) {
Name::delete($id);
echo json(Name::findAll());
});
$app->get('/', function() use($app) {
$app->render('home.tpl.php');
});
$app->run();
<?php
session_start();
if (!isset($_SESSION['names']))
$_SESSION['names'] = array("jane","jim","john","emily","bill","sara");
class Name
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
public function create()
{
$_SESSION['names'][] = $this->name;
}
public function update()
{
$_SESSION['names'][$this->id] = $this->name;
}
public static function delete($id)
{
unset($_SESSION['names'][$id]);
}
public static function findAll()
{
$names = array();
foreach($_SESSION['names'] as $id => $name)
$names[] = new Name($id , $name);
return $names;
}
public static function find($id)
{
return new Name($id, $_SESSION['names'][$id]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment