Slideshow transcript
Slide 1: Javascript REST with Jester
Slide 2: mikebailey mike@bailey.net.au deprec awesome new client Rails app launching soon available for interesting work
Slide 3: the problem • gmap based content management app • didn’t want to reload page • wanted to keep controller simple
Slide 4: enter the jester • http://giantrobots.thoughtbot.com/ • jester.js => 657 lines of javascript • javascript access to your models via REST
Slide 5: How much work does it take to implement?
Slide 6: Not much!
Slide 7: <%= javascript_include_tag :defaults %> <%= javascript_include_tag 'jester.js' %> <%= javascript_include_tag 'demo.js' %>
Slide 8: // demo.js // Object definitions Base.model('PaperRound'); Base.model('Property'); Base.model('Subscription');
Slide 9: Javascript REST in 60 seconds
Slide 10: # Create our application rails demo cd 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_development mysqladmin -u root create demo_development rake db:migrate
Slide 11: # 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/
Slide 12: # Let's go! ./script/server open http://localhost:3000/properties
Slide 13: • interactive javascript console
Slide 14: # 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; # => null property.save(); property.id; # => 4
Slide 15: # create property = Property.create( {name: 'Kim', address: 'Meadowvale'} ); property.id;
Slide 16: # index properties = Property.find('all'); properties.pluck('name');
Slide 17: # show property = Property.find(2); alert('address = ' + property.address);
Slide 18: # update property = Property.find(1); property.address = 'Ridgecrest Retirement Village'; property.save();
Slide 19: # destroy Property.find('all').last().destroy();
Slide 20: # reload a model property.reload(); # can cause problems if your object has associations # as it recreates objects - existing references to # the associated objects are orphaned
Slide 21: Associations
Slide 22: class PropertyController < ApplicationController # 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
Slide 23: Other Tips Use Rails’s .to_xml, not .to_json Changeset 7156 Patch in core for .to_xml clash between :include & :methods
Slide 24: 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



Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 3 (more)