-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for validity of models on the server, and add an example.
- Loading branch information
Showing
7 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
$:.unshift File.expand_path('../../lib', __FILE__) | ||
|
||
require 'sinatra/base' | ||
require 'sequel' | ||
require 'sinatra/backbone' | ||
|
||
DB = Sequel.connect("sqlite::memory:") | ||
DB.create_table :books do | ||
primary_key :id | ||
String :title | ||
String :author | ||
end | ||
|
||
class Book < Sequel::Model | ||
def to_hash | ||
{ :id => id, :title => title, :author => author } | ||
end | ||
|
||
def validate | ||
errors.add :author, "can't be empty" if author.to_s.size == 0 | ||
end | ||
end | ||
|
||
class App < Sinatra::Base | ||
enable :raise_errors, :logging | ||
enable :show_exceptions if development? | ||
|
||
register Sinatra::RestAPI | ||
|
||
rest_create("/book") { Book.new } | ||
rest_resource("/book/:id") { |id| Book[id] } | ||
|
||
set :root, File.expand_path('../', __FILE__) | ||
set :views, File.expand_path('../', __FILE__) | ||
set :public, File.expand_path('../public', __FILE__) | ||
|
||
get '/' do | ||
erb :home | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require './app' | ||
App.set :run, false | ||
run App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title></title> | ||
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script> | ||
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js'></script> | ||
<script src='http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js'></script> | ||
<style> | ||
body { font-family: sans-serif; font-size: 13px; line-height: 1.5; background: #d0d0da; } | ||
#messages { width: 400px; margin: 20px auto; background: white; padding: 20px; border: solid 10px #c0c0ca; } | ||
h3 { border-top: dotted 1px #ccc; padding: 20px 20px 0 20px; margin: 20px -20px 0 -20px; color: #46a; } | ||
h3:first-child { border-top: 0; margin-top: 0; padding-top: 0; } | ||
dl { overflow: hidden; } | ||
dt { float: left; width: 120px; margin-right: 10px; text-align: right; color: #aaa; } | ||
h3 { font-family: palatino; font-size: 1.3em; } | ||
</style> | ||
</head> | ||
<body> | ||
<div id="messages"> | ||
</div> | ||
<script src='app.js'></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Here is our Backbone model! | ||
Book = Backbone.Model.extend({ | ||
urlRoot: '/book' | ||
}); | ||
|
||
$(function() { | ||
do_create(); | ||
}); | ||
|
||
function do_create() { | ||
echo("<h3>Creating a book:</h3>"); | ||
|
||
var book = new Book; | ||
book.set({ title: "Darkly Dreaming Dexter", author: "Jeff Lindsay" }); | ||
book.save({}, { | ||
error: onerror, | ||
success: function() { | ||
print_book(book); | ||
echo("<h3>Retrieving the same book:</h3>"); | ||
do_retrieve(book); | ||
} | ||
}); | ||
} | ||
|
||
function do_retrieve(_book) { | ||
var book = new Book({ id: _book.id }); | ||
book.fetch({ | ||
error: onerror, | ||
success: function() { | ||
print_book(book); | ||
do_edit_error(book); | ||
} | ||
}); | ||
} | ||
|
||
function do_edit_error(book) { | ||
echo("<h3>Editing book with an error:</h3>"); | ||
console.log("(You should see an HTTP error right about here:)"); | ||
book.set({ author: '' }); | ||
book.save({}, { | ||
success: onerror, | ||
error: function() { | ||
console.log("(...yep.)"); | ||
echo("...yes, it occured."); | ||
do_edit(book); | ||
} | ||
}); | ||
} | ||
|
||
function do_edit(book) { | ||
echo("<h3>Editing book:</h3>"); | ||
book.set({ author: 'Anne Rice', title: 'The Claiming of Sleeping Beauty' }); | ||
book.save({}, { | ||
error: onerror, | ||
success: function() { | ||
print_book(book); | ||
do_delete(book); | ||
} | ||
}); | ||
} | ||
|
||
function do_delete(book) { | ||
echo("<h3>Deleting book:</h3>"); | ||
book.destroy({ | ||
error: onerror, | ||
success: function() { | ||
echo("Success."); | ||
do_verify_delete(book.id); | ||
} | ||
}); | ||
} | ||
|
||
function do_verify_delete(id) { | ||
echo("<h3>Checking if book "+id+" still exists:</h3>"); | ||
console.log("(You should see an HTTP error right about here:)"); | ||
var book = new Book({ id: id }); | ||
book.fetch({ | ||
success: onerror, | ||
error: function() { | ||
console.log("(...yep.)"); | ||
echo("No, it doesn't."); | ||
do_success(); | ||
} | ||
}); | ||
} | ||
|
||
function do_success() { | ||
echo("<h3>Success!</h3>"); | ||
} | ||
|
||
function print_book(book) { | ||
echo("<dl><dt>Title:</dt><dd>"+book.get('title')+"</dd></dl>"); | ||
echo("<dl><dt>Author:</dt><dd>"+book.get('author')+"</dd></dl>"); | ||
echo("<dl><dt>ID:</dt><dd>"+book.get('id')+"</dd></dl>"); | ||
} | ||
|
||
// Helper functions | ||
function echo(html) { | ||
$("#messages").append(html); | ||
}; | ||
|
||
function onerror() { | ||
echo("<p class='error'>Oops... an error occured.</p>"); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module Sinatra | ||
module Backbone | ||
def self.version | ||
"0.1.0.rc1" | ||
"0.1.0.rc2" | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters