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
Javascript RESTwith Jester
deprec
awesome new client Rails app launching soon
available for interesting work
the problem
• gmap based content management app
• didn’t want to reload page
• wanted to keep controller simple
enter the jester
• http://giantrobots.thoughtbot.com/
• jester.js => 657 lines of javascript
• javascript access to your models via REST
How much work does it take to implement?
Not much!
<%= javascript_include_tag 'jester.js' %>
<%= javascript_include_tag 'demo.js' %>
<%= javascript_include_tag :defaults %>
// demo.js
// Object definitionsBase.model('PaperRound');Base.model('Property');Base.model('Subscription');
Javascript REST in 60 seconds
# 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
# 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/
# Let's go!
./script/serveropen http://localhost:3000/properties
• interactive javascript console
# 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
# createproperty = Property.create( {name: 'Kim', address: 'Meadowvale'} );property.id;
# indexproperties = Property.find('all');properties.pluck('name');
# showproperty = Property.find(2);alert('address = ' + property.address);
# updateproperty = Property.find(1);property.address = 'Ridgecrest Retirement Village';property.save();
# destroyProperty.find('all').last().destroy();
# reload a modelproperty.reload();
# can cause problems if your object has associations# as it recreates objects - existing references to # the associated objects are orphaned
Associations
# 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
Use Rails’s .to_xml, not .to_json
Changeset 7156
Patch in core for .to_xml clash between :include & :methods
Other Tips
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