SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
Javascript REST
     with Jester
mikebailey
 mike@bailey.net.au




                 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 :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
./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
# 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/
# 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 core

property = Property.build(
  {name: 'Kevin', address: '40 The Avenue, Windsor'} );
property.id;      # => null
property.save();
property.id;      # => 4
# 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 - existing references to
# the associated objects are orphaned
Associations
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
Other Tips




             Use Rails’s .to_xml, not .to_json


          Changeset 7156
Patch in core for .to_xml clash
 between :include & :methods
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

Mais conteúdo relacionado

Mais procurados

AngularJS Introduction
AngularJS IntroductionAngularJS Introduction
AngularJS Introduction
Brajesh Yadav
 

Mais procurados (20)

MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and ExpressMIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
 
Introduction to ember js
Introduction to ember jsIntroduction to ember js
Introduction to ember js
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
Angular Classy
Angular ClassyAngular Classy
Angular Classy
 
No SQL with Kendo UI
No SQL with Kendo UI No SQL with Kendo UI
No SQL with Kendo UI
 
Introduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsIntroduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.js
 
Amitesh Madhur - Web workers, HTML 5
Amitesh Madhur - Web workers, HTML 5Amitesh Madhur - Web workers, HTML 5
Amitesh Madhur - Web workers, HTML 5
 
Controller in AngularJS
Controller in AngularJSController in AngularJS
Controller in AngularJS
 
jQuery Intro
jQuery IntrojQuery Intro
jQuery Intro
 
Building AngularJS Custom Directives
Building AngularJS Custom DirectivesBuilding AngularJS Custom Directives
Building AngularJS Custom Directives
 
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
JavaScripters Event Oct 22, 2016 · 2:00 PM: Common Mistakes made by Angular D...
 
Let's do SPA
Let's do SPALet's do SPA
Let's do SPA
 
Developing Social VR with Open Source Software
Developing Social VR with Open Source SoftwareDeveloping Social VR with Open Source Software
Developing Social VR with Open Source Software
 
AngularJS Introduction
AngularJS IntroductionAngularJS Introduction
AngularJS Introduction
 
SwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsSwiftUI and Combine All the Things
SwiftUI and Combine All the Things
 
idlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHubidlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHub
 
idlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHubidlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHub
 
idlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHubidlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHub
 
idlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHubidlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHub
 
idlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHubidlesign/django-sitecats · GitHub
idlesign/django-sitecats · GitHub
 

Destaque

Ignite Talk Fun4paws
Ignite Talk Fun4pawsIgnite Talk Fun4paws
Ignite Talk Fun4paws
Mike Bailey
 

Destaque (7)

Fixing Victoria: One Law at a Time
Fixing Victoria: One Law at a TimeFixing Victoria: One Law at a Time
Fixing Victoria: One Law at a Time
 
Go Faster, Webmaster
Go Faster, WebmasterGo Faster, Webmaster
Go Faster, Webmaster
 
Go Faster, Webmasters
Go Faster, WebmastersGo Faster, Webmasters
Go Faster, Webmasters
 
Chef
ChefChef
Chef
 
Beer Bottle Night Lamp
Beer Bottle Night LampBeer Bottle Night Lamp
Beer Bottle Night Lamp
 
Ignite Talk Fun4paws
Ignite Talk Fun4pawsIgnite Talk Fun4paws
Ignite Talk Fun4paws
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post Formats
 

Semelhante a Javascript REST with Jester

Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Paulo Ragonha
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 

Semelhante a Javascript REST with Jester (20)

Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
 
Ruby on Rails For .Net Programmers
Ruby on Rails For .Net ProgrammersRuby on Rails For .Net Programmers
Ruby on Rails For .Net Programmers
 
Launching Beeline with Firebase
Launching Beeline with FirebaseLaunching Beeline with Firebase
Launching Beeline with Firebase
 
Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3Migrating Legacy Rails Apps to Rails 3
Migrating Legacy Rails Apps to Rails 3
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
WCLA12 JavaScript
WCLA12 JavaScriptWCLA12 JavaScript
WCLA12 JavaScript
 
Neoito — React 101
Neoito — React 101Neoito — React 101
Neoito — React 101
 
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011 Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
Be lazy, be ESI: HTTP caching and Symfony2 @ PHPDay 2011 05-13-2011
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
Rails 3.1 Asset Pipeline
Rails 3.1 Asset PipelineRails 3.1 Asset Pipeline
Rails 3.1 Asset Pipeline
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected World
 

Último

Último (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Javascript REST with Jester

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