Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Javascript REST
     with Jester
mikebailey
 mike@bailey.net.au




                 deprec
     awesome new client Rails app launching soon

             ...
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...
How much work does it take to implement?
Not much!
<%= javascript_include_tag :defaults %>

<%= javascript_include_tag 'jester.js' %>

<%= javascript_include_tag 'demo.js' %>
// demo.js

// Object definitions
Base.model('PaperRound');
Base.model('Property');
Base.model('Subscription');
Javascript REST in 60 seconds
# Create our application

rails demo
cd demo/
./script/generate scaffold_resource Property name:string
address:string
./sc...
# put in some static content

rm   app/views/layouts/* public/index.html

echo '
<html><head></head><body>
<%= [:propertie...
# Let's go!

./script/server
open 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 cor...
# create
property = Property.create(
  {name: 'Kim', address: 'Meadowvale'} );
property.id;
# index
properties = Property.find('all');
properties.pluck('name');
# show
property = Property.find(2);
alert('address = ' + property.address);
# update
property = Property.find(1);
property.address = 'Ridgecrest Retirement Village';
property.save();
# destroy
Property.find('all').last().destroy();
# reload a model
property.reload();

# can cause problems if your object has associations
# as it recreates objects - exis...
Associations
class PropertyController < ApplicationController


# GET /properties
# GET /properties.xml
def index
  respond_to do |form...
Other Tips




             Use Rails’s .to_xml, not .to_json


          Changeset 7156
Patch in core for .to_xml clash
 ...
more info

•   jester is documented in three blog posts

•   http://giantrobots.thoughtbot.com/

•   I’ve linked to them o...
Nächste SlideShare
Wird geladen in …5
×

Javascript REST with Jester

4.307 Aufrufe

Veröffentlicht am

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.

Veröffentlicht in: Technologie, Business
  • Als Erste(r) kommentieren

Javascript REST with Jester

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

×