SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
Alex Gaziev’s
GEM GON
@gazay (github, twitter)
Saturday, June 8, 13
Saturday, June 8, 13
SURFING
JAZZ
OSS
:russia => ‘BALI’
Saturday, June 8, 13
Some of my works
gon
talks
gon-sinatra
tonik
is
hockeypuck
...
Saturday, June 8, 13
A little about GON
Send data from action to JS
Started 2 years ago
~800 stars
50+ forks and PRs
Saturday, June 8, 13
Data to JS:
data-attribute
Instance variable in controller
Data-attribute in view
Find tag and parse data attribute
Saturday, June 8, 13
Data to JS:
data-attr example
controller
# home_controller.rb
def index
# some logic
@products = counted_products_collection
end
Saturday, June 8, 13
Data to JS:
data-attr example
view
# views/home/index.html.erb
... some content
<%= contant_tag “div”, id: “products”, data:
{ products: @products } do %>
Loading products...
<% end %>
Saturday, June 8, 13
Data to JS:
data-attr example
JS
# assets/javascripts/pages/home.js.coffee
jQuery ->
doSomething $(‘#products’).data(‘products’)
Saturday, June 8, 13
Data to JS:
javascript_tag
Excess lines inside your views
You should think what you write
Saturday, June 8, 13
Data to JS:
javascript_tag
view
# views/home/index.html.erb
... some content
<%= javascript_tag do %>
window.prodsURL = “<%=j products_url %>”
window.prods = <%=raw @prods.to_json %>
<% end %>
Saturday, June 8, 13
Data to JS:
javascript_tag
JS
# assets/javascripts/pages/home.js.coffee
jQuery ->
doSomething prods
Saturday, June 8, 13
Saturday, June 8, 13
Data to JS
GON
Views step - once (perfect - in layout)
Don’t have to think about escaping
Stored uniq data per request
Saturday, June 8, 13
Data to JS:
GON example
controller
# home_controller.rb
def index
# some logic
gon.products = counted_collection
gon.products_url = products_url
end
Saturday, June 8, 13
Data to JS:
GON example
view
# views/layouts/layout.html.erb
<head>
<%= include_gon(options) %>
... your js, css etc
Saturday, June 8, 13
Data to JS:
GON example
JS
# assets/javascripts/pages/home.js.coffee
jQuery ->
doSomething gon.products
Saturday, June 8, 13
Usual ways: cons
Escaping ( “</script><script>evil()</
script>”)
Excess lines in your views
Search for tag and parse attributes
Views step
Saturday, June 8, 13
GON: pros
Automatically escape and transform
to_json all data
What you write in action – you’ll get in JS
Should modify views once
Saturday, June 8, 13
GON. Next versions
Generate json with template engines
Share data between requests
Renew data in JS gon easy way
Saturday, June 8, 13
GON
RABL and jBuilder
Success!
Screencast
Ruby Weekly
More users => more PRs
Saturday, June 8, 13
GON, RABL and
jBuilder
why so cool?
All power of RABL and jBuilder
Clean your controller new way
Render multiple templates with GON
Saturday, June 8, 13
GON and RABL
multi templates
# home_controller.rb
def index
gon.rabl ‘path_to_temlpate1’, as: ‘posts’
gon.rabl ‘path_to_template2’, as: ‘users’
gon.rabl ‘path_to_template3’, as: ‘admins’
gon.rabl # app/views/home/index.rabl
end
Saturday, June 8, 13
GON and jBuilder
problems
jBuilder rendering ïŹ‚ow
Metaprogramming
Whole environment for eval
Saturday, June 8, 13
Saturday, June 8, 13
GON Global
Share data application-wide
Initial data
Change settings everywhere by event
Saturday, June 8, 13
GON Global
init data example
# conïŹg/initializers/gon.rb
Gon.global.pictures_per_user = 4
# app/assets/javascripts/imageboard.js.coffee
setLimitToUser = gon.global.pictures_per_request
Saturday, June 8, 13
GON Global
change data example
# app/model/user.rb
after_save :change_users_limit
def change_users_limit
if User.count > 1000
Gon.global.pictures_per_request = 3
end
end
Saturday, June 8, 13
GON watch
Renew data for variable
No excess LOC
Use current state of action
Saturday, June 8, 13
GON watch:
how it works
# home_controller.rb
def index
num = User.count * factor + params[:some].to_i
gon.watch.pictures_per_request = num
gon.watch.other_num = num * factor2
end
Saturday, June 8, 13
GON watch:
how it works
# layout.html.erb
<head>
<%= include_gon(watch: true) %>
...
Saturday, June 8, 13
GON watch:
how it works
# home_index.js.coffee
renewLimit = (count) ->
$(‘#limit-counter’).text(count)
gon.watch(‘pictures_per_request’, interval: 1000,
renewLimit)
Saturday, June 8, 13
GON watch
example
Saturday, June 8, 13
Thank you!
Any questions?
http://github.com/gazay/gon
Saturday, June 8, 13

Weitere Àhnliche Inhalte

Ähnlich wie Gon gem. For RDRC 2013, June 7

Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ Android
Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ AndroidĐ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ Android
Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ AndroidSoftTechnics
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSPablo Godel
 
Intro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyIntro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyAzat Mardanov
 
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)Red Hat Developers
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e bigAndy Peterson
 
Oredev 2013: Building Web Apps with Ember.js
Oredev 2013: Building Web Apps with Ember.jsOredev 2013: Building Web Apps with Ember.js
Oredev 2013: Building Web Apps with Ember.jsJesse Cravens
 
UberFire Quick Intro and Overview (early beta Aug 2013)
UberFire Quick Intro and Overview (early beta Aug 2013)UberFire Quick Intro and Overview (early beta Aug 2013)
UberFire Quick Intro and Overview (early beta Aug 2013)Mark Proctor
 
jQuery Mobile Deep Dive
jQuery Mobile Deep DivejQuery Mobile Deep Dive
jQuery Mobile Deep DiveTroy Miles
 
EinfĂŒhrung in AngularJS
EinfĂŒhrung in AngularJSEinfĂŒhrung in AngularJS
EinfĂŒhrung in AngularJSSebastian Springer
 
A Better UJS for Rails
A Better UJS for RailsA Better UJS for Rails
A Better UJS for RailsZack Siri
 
Performance and Optmization - a technical talk at Frontend London
Performance and Optmization - a technical talk at Frontend LondonPerformance and Optmization - a technical talk at Frontend London
Performance and Optmization - a technical talk at Frontend Londonthomas alisi
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.jsJay Phelps
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptjasonsich
 
Dive into AngularJS and directives
Dive into AngularJS and directivesDive into AngularJS and directives
Dive into AngularJS and directivesTricode (part of Dept)
 
Passing a Front end Developer interview
Passing a Front end Developer interview Passing a Front end Developer interview
Passing a Front end Developer interview tonyfarnsworth
 
What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...Richard McIntyre
 
Data visualization in python/Django
Data visualization in python/DjangoData visualization in python/Django
Data visualization in python/Djangokenluck2001
 

Ähnlich wie Gon gem. For RDRC 2013, June 7 (20)

Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ Android
Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ AndroidĐ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ Android
Đ˜ŃĐżĐŸĐ»ŃŒĐ·ĐŸĐČĐ°ĐœĐžĐ” Debug ŃƒŃ‚ĐžĐ»ĐžŃ‚ ĐČ Ń€Đ°Đ·Ń€Đ°Đ±ĐŸŃ‚ĐșĐ” ĐżĐŸĐŽ Android
 
Tek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJSTek 2013 - Building Web Apps from a New Angle with AngularJS
Tek 2013 - Building Web Apps from a New Angle with AngularJS
 
Intro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General AssemblyIntro to Backbone.js by Azat Mardanov for General Assembly
Intro to Backbone.js by Azat Mardanov for General Assembly
 
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
Boost Development With Java EE7 On EAP7 (Demitris Andreadis)
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
Oredev 2013: Building Web Apps with Ember.js
Oredev 2013: Building Web Apps with Ember.jsOredev 2013: Building Web Apps with Ember.js
Oredev 2013: Building Web Apps with Ember.js
 
UberFire Quick Intro and Overview (early beta Aug 2013)
UberFire Quick Intro and Overview (early beta Aug 2013)UberFire Quick Intro and Overview (early beta Aug 2013)
UberFire Quick Intro and Overview (early beta Aug 2013)
 
jQuery Mobile Deep Dive
jQuery Mobile Deep DivejQuery Mobile Deep Dive
jQuery Mobile Deep Dive
 
EinfĂŒhrung in AngularJS
EinfĂŒhrung in AngularJSEinfĂŒhrung in AngularJS
EinfĂŒhrung in AngularJS
 
Slides
SlidesSlides
Slides
 
Phplx mongodb
Phplx mongodbPhplx mongodb
Phplx mongodb
 
A Better UJS for Rails
A Better UJS for RailsA Better UJS for Rails
A Better UJS for Rails
 
RequireJS
RequireJSRequireJS
RequireJS
 
Performance and Optmization - a technical talk at Frontend London
Performance and Optmization - a technical talk at Frontend LondonPerformance and Optmization - a technical talk at Frontend London
Performance and Optmization - a technical talk at Frontend London
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
Intro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScriptIntro to React - Featuring Modern JavaScript
Intro to React - Featuring Modern JavaScript
 
Dive into AngularJS and directives
Dive into AngularJS and directivesDive into AngularJS and directives
Dive into AngularJS and directives
 
Passing a Front end Developer interview
Passing a Front end Developer interview Passing a Front end Developer interview
Passing a Front end Developer interview
 
What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...What is this DI and AOP stuff anyway...
What is this DI and AOP stuff anyway...
 
Data visualization in python/Django
Data visualization in python/DjangoData visualization in python/Django
Data visualization in python/Django
 

KĂŒrzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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...Martijn de Jong
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

KĂŒrzlich hochgeladen (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls đŸ„° 8617370543 Service Offer VIP Hot Model
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Gon gem. For RDRC 2013, June 7

  • 1. Alex Gaziev’s GEM GON @gazay (github, twitter) Saturday, June 8, 13
  • 4. Some of my works gon talks gon-sinatra tonik is hockeypuck ... Saturday, June 8, 13
  • 5. A little about GON Send data from action to JS Started 2 years ago ~800 stars 50+ forks and PRs Saturday, June 8, 13
  • 6. Data to JS: data-attribute Instance variable in controller Data-attribute in view Find tag and parse data attribute Saturday, June 8, 13
  • 7. Data to JS: data-attr example controller # home_controller.rb def index # some logic @products = counted_products_collection end Saturday, June 8, 13
  • 8. Data to JS: data-attr example view # views/home/index.html.erb ... some content <%= contant_tag “div”, id: “products”, data: { products: @products } do %> Loading products... <% end %> Saturday, June 8, 13
  • 9. Data to JS: data-attr example JS # assets/javascripts/pages/home.js.coffee jQuery -> doSomething $(‘#products’).data(‘products’) Saturday, June 8, 13
  • 10. Data to JS: javascript_tag Excess lines inside your views You should think what you write Saturday, June 8, 13
  • 11. Data to JS: javascript_tag view # views/home/index.html.erb ... some content <%= javascript_tag do %> window.prodsURL = “<%=j products_url %>” window.prods = <%=raw @prods.to_json %> <% end %> Saturday, June 8, 13
  • 12. Data to JS: javascript_tag JS # assets/javascripts/pages/home.js.coffee jQuery -> doSomething prods Saturday, June 8, 13
  • 14. Data to JS GON Views step - once (perfect - in layout) Don’t have to think about escaping Stored uniq data per request Saturday, June 8, 13
  • 15. Data to JS: GON example controller # home_controller.rb def index # some logic gon.products = counted_collection gon.products_url = products_url end Saturday, June 8, 13
  • 16. Data to JS: GON example view # views/layouts/layout.html.erb <head> <%= include_gon(options) %> ... your js, css etc Saturday, June 8, 13
  • 17. Data to JS: GON example JS # assets/javascripts/pages/home.js.coffee jQuery -> doSomething gon.products Saturday, June 8, 13
  • 18. Usual ways: cons Escaping ( “</script><script>evil()</ script>”) Excess lines in your views Search for tag and parse attributes Views step Saturday, June 8, 13
  • 19. GON: pros Automatically escape and transform to_json all data What you write in action – you’ll get in JS Should modify views once Saturday, June 8, 13
  • 20. GON. Next versions Generate json with template engines Share data between requests Renew data in JS gon easy way Saturday, June 8, 13
  • 21. GON RABL and jBuilder Success! Screencast Ruby Weekly More users => more PRs Saturday, June 8, 13
  • 22. GON, RABL and jBuilder why so cool? All power of RABL and jBuilder Clean your controller new way Render multiple templates with GON Saturday, June 8, 13
  • 23. GON and RABL multi templates # home_controller.rb def index gon.rabl ‘path_to_temlpate1’, as: ‘posts’ gon.rabl ‘path_to_template2’, as: ‘users’ gon.rabl ‘path_to_template3’, as: ‘admins’ gon.rabl # app/views/home/index.rabl end Saturday, June 8, 13
  • 24. GON and jBuilder problems jBuilder rendering ïŹ‚ow Metaprogramming Whole environment for eval Saturday, June 8, 13
  • 26. GON Global Share data application-wide Initial data Change settings everywhere by event Saturday, June 8, 13
  • 27. GON Global init data example # conïŹg/initializers/gon.rb Gon.global.pictures_per_user = 4 # app/assets/javascripts/imageboard.js.coffee setLimitToUser = gon.global.pictures_per_request Saturday, June 8, 13
  • 28. GON Global change data example # app/model/user.rb after_save :change_users_limit def change_users_limit if User.count > 1000 Gon.global.pictures_per_request = 3 end end Saturday, June 8, 13
  • 29. GON watch Renew data for variable No excess LOC Use current state of action Saturday, June 8, 13
  • 30. GON watch: how it works # home_controller.rb def index num = User.count * factor + params[:some].to_i gon.watch.pictures_per_request = num gon.watch.other_num = num * factor2 end Saturday, June 8, 13
  • 31. GON watch: how it works # layout.html.erb <head> <%= include_gon(watch: true) %> ... Saturday, June 8, 13
  • 32. GON watch: how it works # home_index.js.coffee renewLimit = (count) -> $(‘#limit-counter’).text(count) gon.watch(‘pictures_per_request’, interval: 1000, renewLimit) Saturday, June 8, 13