Javascript REST with Jester

24
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 of Javascript REST with Jester

Page 1: Javascript REST with Jester

Javascript RESTwith Jester

Page 2: Javascript REST with Jester

[email protected]

deprec

awesome new client Rails app launching soon

available for interesting work

Page 3: Javascript REST with Jester

the problem

• gmap based content management app

• didn’t want to reload page

• wanted to keep controller simple

Page 4: Javascript REST with Jester

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

How much work does it take to implement?

Page 6: Javascript REST with Jester

Not much!

Page 7: Javascript REST with Jester

<%= javascript_include_tag 'jester.js' %>

<%= javascript_include_tag 'demo.js' %>

<%= javascript_include_tag :defaults %>

Page 8: Javascript REST with Jester

// demo.js

// Object definitionsBase.model('PaperRound');Base.model('Property');Base.model('Subscription');

Page 9: Javascript REST with Jester

Javascript REST in 60 seconds

Page 10: Javascript REST with Jester

# 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

# 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

# Let's go!

./script/serveropen http://localhost:3000/properties

Page 13: Javascript REST with Jester

• interactive javascript console

Page 14: Javascript REST with Jester

# 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

# createproperty = Property.create( {name: 'Kim', address: 'Meadowvale'} );property.id;

Page 16: Javascript REST with Jester

# indexproperties = Property.find('all');properties.pluck('name');

Page 17: Javascript REST with Jester

# showproperty = Property.find(2);alert('address = ' + property.address);

Page 18: Javascript REST with Jester

# updateproperty = Property.find(1);property.address = 'Ridgecrest Retirement Village';property.save();

Page 19: Javascript REST with Jester

# destroyProperty.find('all').last().destroy();

Page 20: Javascript REST with Jester

# 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

Associations

Page 22: Javascript REST with Jester

# 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

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

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