javascript rest with jester
DESCRIPTION
Mike Bailey gave a presentation on a Javascript library called Jester to the Melbourne Ruby Users Group. Here are the slides. They contain mostly working code.TRANSCRIPT
![Page 1: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/1.jpg)
Javascript RESTwith Jester
![Page 2: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/2.jpg)
deprec
awesome new client Rails app launching soon
available for interesting work
![Page 3: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/3.jpg)
the problem
• gmap based content management app
• didn’t want to reload page
• wanted to keep controller simple
![Page 4: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/4.jpg)
enter the jester
• http://giantrobots.thoughtbot.com/
• jester.js => 657 lines of javascript
• javascript access to your models via REST
![Page 5: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/5.jpg)
How much work does it take to implement?
![Page 6: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/6.jpg)
Not much!
![Page 7: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/7.jpg)
<%= javascript_include_tag 'jester.js' %>
<%= javascript_include_tag 'demo.js' %>
<%= javascript_include_tag :defaults %>
![Page 8: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/8.jpg)
// demo.js
// Object definitionsBase.model('PaperRound');Base.model('Property');Base.model('Subscription');
![Page 9: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/9.jpg)
Javascript REST in 60 seconds
![Page 10: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/10.jpg)
# Create our application
rails democd demo/./script/generate scaffold_resource Property name:string address:string./script/generate scaffold_resource PaperRound name:string./script/generate scaffold_resource Subscription paper_round_id:integer property_id:integer position:integer
# Setup our database
mysqladmin -u root drop demo_developmentmysqladmin -u root create demo_developmentrake db:migrate
![Page 11: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/11.jpg)
# put in some static content
rm app/views/layouts/* public/index.html
echo '<html><head></head><body><%= [:properties, :paper_rounds, :subscriptions].collect {|i| link_to(i, send("#{i}_path"))}.join(" | ") %><%= yield %><%= javascript_include_tag :defaults %><%= javascript_include_tag "jester.js" %><%= javascript_include_tag "demo.js" %></body></html>' > app/views/layouts/application.rhtml
echo “Base.model('PaperRound');Base.model('Property');Base.model('Subscription');” > public/javascripts/demo.js
cp ~/jester.js public/javascripts/
![Page 12: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/12.jpg)
# Let's go!
./script/serveropen http://localhost:3000/properties
![Page 13: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/13.jpg)
• interactive javascript console
![Page 14: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/14.jpg)
# new # note, reflection available but not turned on by default # rails patch by jesters author has been accepted into core
property = Property.build( {name: 'Kevin', address: '40 The Avenue, Windsor'} ); property.id; # => nullproperty.save();property.id; # => 4
![Page 15: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/15.jpg)
# createproperty = Property.create( {name: 'Kim', address: 'Meadowvale'} );property.id;
![Page 16: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/16.jpg)
# indexproperties = Property.find('all');properties.pluck('name');
![Page 17: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/17.jpg)
# showproperty = Property.find(2);alert('address = ' + property.address);
![Page 18: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/18.jpg)
# updateproperty = Property.find(1);property.address = 'Ridgecrest Retirement Village';property.save();
![Page 19: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/19.jpg)
# destroyProperty.find('all').last().destroy();
![Page 20: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/20.jpg)
# reload a modelproperty.reload();
# can cause problems if your object has associations# as it recreates objects - existing references to # the associated objects are orphaned
![Page 21: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/21.jpg)
Associations
![Page 22: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/22.jpg)
# GET /properties # GET /properties.xml def index respond_to do |format| format.html # index.rhtml format.xml { render :xml => @properties.to_xml( :include => [:subscriptions], # associated models :methods => [:amount_owing] # call these methods ) } end end
class PropertyController < ApplicationController
![Page 23: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/23.jpg)
Use Rails’s .to_xml, not .to_json
Changeset 7156
Patch in core for .to_xml clash between :include & :methods
Other Tips
![Page 24: Javascript REST with Jester](https://reader034.vdocument.in/reader034/viewer/2022042714/54b6b1f94a79597b0c8b45fd/html5/thumbnails/24.jpg)
more info
• jester is documented in three blog posts
• http://giantrobots.thoughtbot.com/
• I’ve linked to them on my blog
• http://mike.bailey.net.au/blog/?p=15