Gonã®ä½¿ãæ¹ ã¾ã¨ã
- Gonã¨ã¯
- å°å ¥
- è¨å®
- 使ãæ¹
- watch: watchæ©è½ãæå¹åãã
- camel_case: ãã£ã¡ã«ã±ã¼ã¹ã«å¤æãã
- camel_depth: ãã£ã¡ã«ã±ã¼ã¹ãé©ç¨ããæ·±ã
- namespace: gonã¨ããåå空éãå¤ãã
- init: window.gon = {}ã§åæåãã
- type: <script>ã«type="text/javascript"ã追å ãã
- nonce: <script>ã«nonce=...ã追å ãã
- need_tag: <script>ã¿ã°ãã
- cdata: CDATAãã
- global_root: globalã¨ããåå空éãå¤ãã
- amd: AMD対å¿
- <script>ã¿ã°ã®renderã¡ã½ãã*3
- ã¡ã½ãã
- watchæ©è½
- ã°ãã¼ãã«ã«ä½¿ã
- RSpecã§ã³ã³ããã¼ã©ã¼ãã¹ã
- ãã£ããã³ã¼ããªã¼ãã£ã³ã°
- åèURL
- èªåç¨ã®ã¡ã¢ãå ¬éãããã®ã§ããããã¥ã¡ã³ã/ã½ã¼ã¹ã³ã¼ãçãã¾ã¨ããã ãã§è©¦ãã¦ããªãã³ã¼ããå¤ãã®ã§ãä¿¡é ¼æ§ã¯ä½ãã§ãã
Gonã¨ã¯
- Railsã®å¤æ°ãJSã§ä½¿ããããã«ãªã
å°å ¥
1. ã¤ã³ã¹ãã¼ã«
# Gemfile gem 'gon'
$ bundle
2. ã»ããã¢ãã
# application.html.erb <head> ... # application.jsã®åã«èªã¿è¾¼ãã°ãã¼ãåã«gonå¤æ°ã«ã¢ã¯ã»ã¹ã§ãã # # 以ä¸ã®ãããªHTMLãçæãã¦ããã # <script> # //<![CDATA[ # window.gon={};gon.hoge=1; # //]]> # </script> <%= Gon::Base.render_data %> <%= javascript_include_tag "application" %> ... </head>
3. 使ã£ã¦ã¿ã
class ProductsController < ApplicationController def index # Railså´ã§å¤æ°ãã»ãã gon.hoge = 1 ... end end
# coffee # JSå´ã§ä½¿ç¨ã§ãã # åãã£ã¹ããã¨ã¹ã±ã¼ããããæãã«ãã£ã¦ããã gon.hoge // => 1
è¨å®
使ãæ¹
Gon::Base.render_data
ã®ãªãã·ã§ã³ã¨ãã¦æå®ãã
Gon::Base.render_data(watch: true)
watch: watchæ©è½ãæå¹åãã
- watchæ©è½ ãåç §
- å®éã«ã¯falseã§ãwatchæ©è½ã¯ä½¿ãããæ示çã«å®£è¨ããã ã
- ããã©ã«ã: false
camel_case: ãã£ã¡ã«ã±ã¼ã¹ã«å¤æãã
- ããã©ã«ã: false
# è¨å® Gon::Base.render_data(camel_case: true) # ruby gon.int_cased = 1 # js gon.intCased #=> 1
camel_depth: ãã£ã¡ã«ã±ã¼ã¹ãé©ç¨ããæ·±ã
- ããã©ã«ã: 1
# è¨å® Gon::Base.render_data(:camel_case => true, :camel_depth => 2) # js gon.testHash.testDepthOne.test_depth_two
namespace: gonã¨ããåå空éãå¤ãã
- ããã©ã«ã: 'gon'
# è¨å® Gon::Base.render_data(:namespace => 'serverExports') # js serverExports.your_int
init: window.gon = {}
ã§åæåãã
- ããã©ã«ã: true
# js # åæåããã¦ãã®ã§ããã¼ã¿ããªãå ´åã§ãã¨ã©ã¼ã«ãªããªã window.gon // => {}
type: <script>
ã«type="text/javascript"
ã追å ãã
- false
Gon::Base.render_data(type: true) #=> <script type="text/javascript">window.gon=...</script>
nonce: <script>
ã«nonce=...
ã追å ãã
- CSP対å¿
- ããã©ã«ã: nil
Gon::Base.render_data(nonce: 'test') #=> "<script nonce=\"test">...
need_tag: <script>
ã¿ã°ãã
- ããã©ã«ã: true
Gon::Base.render_data(need_tag: true) #=> <script> #=> //<![CDATA[ #=> window.gon={};gon.hoge="piyo"; #=> //]]> #=> </script>
Gon::Base.render_data(need_tag: false) #=> window.gon={};gon.hoge="piyo";
cdata: CDATAãã
- ããã©ã«ã: true
global_root: globalã¨ããåå空éãå¤ãã
- ããã©ã«ã: 'global'
amd: AMD対å¿
- ããã©ã«ã: false
include_gon_amd
ã¨åã
<script>
ã¿ã°ã®renderã¡ã½ãã*3
Gon::Base.render_data
- åºæ¬ã³ã¬ã使ãã°ok
include_gon
- Rails3ã®å ´åã¯ãã£ã¡ããã
- å
é¨ã§
Gon::Base.render_data
ãå©ç¨ãã¦ã
<%= include_gon %>
include_gon_amd
- AMDã®å ´åã¯ãã£ã¡
- å
é¨ã§
Gon::Base.render_data
ãå©ç¨ãã¦ã
<%= include_gon_amd %>
ã¡ã½ãã
gon.hoge=: set
gon.hoge = 1
gon.push: set
gon.push(hoge: 1, piyo: 2)
gon.hoge: get
gon.hoge #=> 1
gon.all_variables: setããå ¨ã¦ã®å¤
gon.hoge = 1 gon.piyo = 2 gon.all_variables #=> {"hoge"=>1, "piyo"=>2}
gon.clear: setããå¤ãæ¶ã
gon.hoge = 1 gon.piyo = 2 gon.all_variables #=> {"hoge"=>1, "piyo"=>2} gon.clear gon.all_variables #=> {}
watchæ©è½
- Ajaxã§ãã¼ãªã³ã°ãã¦ãgonã®ãã¼ã¿ããªã¢ã«ã¿ã¤ã ã§åå¾ãã
å°å ¥
1. watchãªãã·ã§ã³ãtrueã«ãã
- ã©ãã
watch
ãªãã·ã§ã³ã¯æ示çã«ããæå³ãããªããæå®ããªãã¦ãOKã£ã½ã
# app/views/layouts/application.html.erb <%= include_gon(watch: true) %>
2. ã¢ããªä½æ
- ã³ã³ããã¼ã©ã¼ã§
gon.watch
ãå©ç¨ããã®ããã¤ã³ã
# app/controllers/home_controller.rb # ããã«Ajaxãªã¯ã¨ã¹ããéãããããã®ãã³ã«users_countã®å¤ãã¬ã¹ãã³ã¹ãã¦æ´æ°ããã def index @users_count = User.count gon.watch.users_count = @users_count end
# app/views/home/index.html.erb # Ajaxæåæã«ãã³ã¼ã«ããã¯ã§ããã®è¡¨ç¤ºãå¤ãã <div id='users-counter'></div>
3. JS
gon.watch()
ã¯ä¸å®ééã§ãªã¯ã¨ã¹ããéããã³ã³ããã¼ã©ã¼ã§gonã«ã»ããããå¤æ°ãã³ã¼ã«ããã¯ã§å©ç¨ãã
# app/assets/javascripts/home.js.coffee # ã³ã¼ã«ãã㯠# uesrs_count: Ajaxã®ã¬ã¹ãã³ã¹å¤ renewUsers = (uesrs_count) -> $('#users-counter').text(users_count) # gon.watchã®ä½¿ãæ¹ # gon.watchã使ãã¨ãã¼ãªã³ã°ã§ãã # ããã§ã¯1ç§ãã¨ã«`/home`ã«Ajaxãªã¯ã¨ã¹ããéã£ã¦ã`#users-counter`ã®è¡¨ç¤ºãå¤æ´ãã¦ãã # # gon.watch(name_of_variable, options, callback) # name_of_variable: å¤æ°å(ã³ã³ããã¼ã©ã¼ã§`gon.watch.users_count = @users_count`ã¨ããã°ã'users_count'ã¨ãªã) # options # interval: Ajaxãªã¯ã¨ã¹ãããééãms # method: HTTPã¡ã½ãããããã©ã«ãã¯GET # url: é¸ã¹ããéãURL # callback: Ajaxã®ã³ã¼ã«ãã㯠gon.watch('users_count', interval: 1000, renewUsers)
æ¢ãã
gon.unwatch
ã§æ¢ãã
# ãã¥ã¼ <a href='#' id='stop-renewing'> Stop renewing </a>
# coffee $('#stop-renewing').click -> # æ¢ãã gon.unwatch('users_count', renewUsers) return false
ã°ãã¼ãã«ã«ä½¿ã
Gon.global
ã使ã
# config/initializers/some_initializer.rb Gon.global.variable = 1
# coffee gon.global.variable // => 1
RSpecã§ã³ã³ããã¼ã©ã¼ãã¹ã
# spec/support/shared_contexts/gon.rb shared_context :gon do # Gonã¯å é¨ã§RequestStore(ãªã¯ã¨ã¹ãã°ãã¼ãã«ãªå¤æ°ãgem)ãå©ç¨ãã¦ãã # RequestStoreã¯Rackããã«ã¦ã§ã¢ã§å¤æ°ãåé¤ãã # ããããã¹ãã§ã¯Rackããã«ã¦ã§ã¢ãçµç±ããªã # ãã®ãããã¹ãã±ã¼ã¹æ¯ã«Gonã®å¤æ°ãæåã§ã¯ãªã¢ããå¿ è¦ããã # åè: https://tech.misoca.jp/entry/2015/06/15/151419 let(:gon) { RequestStore.store[:gon].gon } before { Gon.clear } end # spec/controllers/thingies_controller_spec.rb RSpec.describe ThingiesController do include_context :gon describe 'GET #new' do it 'gonifies as expected' do get :new, {}, valid_session # Gonã«å¤æ°ãsetããã¦ãããã¨ããã¹ã expect(gon['key']).to eq :value end end end
ãã£ããã³ã¼ããªã¼ãã£ã³ã°
gon.gemspec
- ä¾ågem
- actionpack: Railsã®controller/routing
- request_store: ãªã¯ã¨ã¹ãã°ãã¼ãã«ãªå¤æ°
- multi_json: 主è¦ãªJSON Engineã«å¯¾å¿ããJSONãã¼ãµ
lib/gon.rb
- ã¡ã¤ã³ã¨ãªãGonã¯ã©ã¹
- ã³ã³ããã¼ã©ã¼ã®
gon
ã®æ£ä½ã¯Gon
gon.hoge = 1
ãgon.hoge
ã«ã¯method_missingãå©ç¨ãã¦ããã
lib/gon/base.rb
Gon::Base.render_data
-Gon::Base.render_data
ã®ãªãã·ã§ã³ã®ããã©ã«ãå¤
VALID_OPTION_DEFAULTS = { namespace: 'gon', camel_case: false, camel_depth: 1, watch: false, need_tag: true, type: false, cdata: true, global_root: 'global', namespace_check: false, amd: false, nonce: nil }
lib/gon/global.rb
- Gon::Globalã¯Gonã®ãµãã¯ã©ã¹
- Gon::Globalã¯gon.globalã«å¯¾å¿
- Gonã¨ã®éãã¯ããã¾ããªãã£ã½ã
lib/gon/watch.rb
- Gon::Watchã¯Gonã®ãµãã¯ã©ã¹
- Gon::Watchã¯gon.watchã«å¯¾å¿
coffee/watch.coffee
gon.watch
ãgon.unwatch
ãgon.unwatchAll
ãå®ç¾©- jQueryä¾åã£ã½ã
js/watch.js
- coffee/watch.coffeeãã³ã³ãã¤ã«ãããã¡ã¤ã«