{"id":13463261,"url":"https://github.com/gazay/gon","last_synced_at":"2025-05-13T15:12:57.248Z","repository":{"id":44499165,"uuid":"1723155","full_name":"gazay/gon","owner":"gazay","description":"Your Rails variables in your JS","archived":false,"fork":false,"pushed_at":"2023-03-10T15:31:07.000Z","size":878,"stargazers_count":3060,"open_issues_count":49,"forks_count":184,"subscribers_count":53,"default_branch":"master","last_synced_at":"2025-05-01T08:09:43.245Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gazay.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"tidelift":"rubygems/gon"}},"created_at":"2011-05-09T15:26:18.000Z","updated_at":"2025-04-26T15:36:24.000Z","dependencies_parsed_at":"2022-09-12T12:03:06.535Z","dependency_job_id":"4196b9f1-51fe-42ec-8ec3-46b78efec765","html_url":"https://github.com/gazay/gon","commit_stats":{"total_commits":372,"total_committers":68,"mean_commits":5.470588235294118,"dds":"0.41666666666666663","last_synced_commit":"ab7e6113d0ba002fe4a7f2b72a423d602805c1ce"},"previous_names":[],"tags_count":54,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazay%2Fgon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazay%2Fgon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazay%2Fgon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gazay%2Fgon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gazay","download_url":"https://codeload.github.com/gazay/gon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252316828,"owners_count":21728521,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T13:00:49.215Z","updated_at":"2025-05-13T15:12:57.208Z","avatar_url":"https://github.com/gazay.png","language":"Ruby","readme":"# Gon gem — get your Rails variables in your js\n\n[![Join the chat at https://gitter.im/gazay/gon](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gazay/gon?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n![Gon. You should try this. If you look closer - you will see an elephant.](https://github.com/gazay/gon/raw/master/doc/logo_small.png)\n\n[![Build Status](https://travis-ci.org/gazay/gon.svg?branch=master)](https://travis-ci.org/gazay/gon) [![CodeClimate](https://codeclimate.com/github/gazay/gon/badges/gpa.svg)](https://codeclimate.com/github/gazay/gon)\n\nIf you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force!\n\nNow you can easily renew data in your variables through ajax with [gon.watch](https://github.com/gazay/gon/wiki/Usage-gon-watch)!\n\nWith [Jbuilder](https://github.com/rails/jbuilder), [Rabl](https://github.com/nesquena/rabl), and [Rabl-Rails](https://github.com/ccocchi/rabl-rails) support!\n\nFor Sinatra available [gon-sinatra](https://github.com/gazay/gon-sinatra).\n\nFor .Net MVC available port [NGon](https://github.com/brooklynDev/NGon).\n\nFor elixir Phoenix available [PhoenixGon](https://github.com/khusnetdinov/phoenix_gon).\n\n\u003ca href=\"https://evilmartians.com/?utm_source=gon\"\u003e\n\u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\" alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\n\u003c/a\u003e\n\n## An example of typical use\n\n### Very good and detailed example and reasons to use is considered in [railscast](http://railscasts.com/episodes/324-passing-data-to-javascript) by Ryan Bates\n\nWhen you need to send some start data from your controller to your js\nyou might be doing something like this:\n\n  1. Write this data in controller(presenter/model) to some variable\n  2. In view for this action you put this variable to some objects by data\n     attributes, or write js right in view\n  3. Then there can be two ways in js:\n    + if you previously wrote data in data\n     attributes - you should parse this attributes and write data to some\n     js variable.\n    + if you wrote js right in view (many frontenders would shame you for\n  that) - you just use data from this js - OK.\n  4. You can use your data in your js\n\nAnd every time when you need to send some data from action to js you do this.\n\nWith gon you configure it firstly - just put in layout one tag, and add\ngem line to your Gemfile and do the following:\n\n  1. Write variables by\n\n   ``` ruby\n    gon.variable_name = variable_value\n\n    # or new syntax\n    gon.push({\n      :user_id =\u003e 1,\n      :user_role =\u003e \"admin\"\n    })\n\n    gon.push(any_object) # any_object with respond_to? :each_pair\n   ```\n\n  2. In your js you get this by\n\n   ``` js\n    gon.variable_name\n   ```\n\n  3. profit?\n\nWith the `gon.watch` feature you can easily renew data in gon variables!\nSimply call `gon.watch` from your js file. It's super useful\nin modern web applications!\n\n## Usage\n\n### More details about configuration and usage you can find in [gon wiki](https://github.com/gazay/gon/wiki)\n\n`app/views/layouts/application.html.erb`\n\n``` erb\n\u003chead\u003e\n  \u003ctitle\u003esome title\u003c/title\u003e\n  \u003c%= Gon::Base.render_data %\u003e\n  \u003c!-- include your action js code --\u003e\n  ...\n```\n\nFor rails 3:\n``` erb\n  \u003c%= include_gon %\u003e\n  ...\n```\n\n\n\nYou can pass some [options](https://github.com/gazay/gon/wiki/Options)\nto `render_data` method.\n\nYou put something like this in the action of your controller:\n\n``` ruby\n@your_int = 123\n@your_array = [1,2]\n@your_hash = {'a' =\u003e 1, 'b' =\u003e 2}\ngon.your_int = @your_int\ngon.your_other_int = 345 + gon.your_int\ngon.your_array = @your_array\ngon.your_array \u003c\u003c gon.your_int\ngon.your_hash = @your_hash\n\ngon.all_variables # \u003e {:your_int =\u003e 123, :your_other_int =\u003e 468, :your_array =\u003e [1, 2, 123], :your_hash =\u003e {'a' =\u003e 1, 'b' =\u003e 2}}\ngon.your_array # \u003e [1, 2, 123]\n\n# gon.clear # gon.all_variables now is {}\n```\n\nAccess the variables from your JavaScript file:\n\n``` js\nalert(gon.your_int)\nalert(gon.your_other_int)\nalert(gon.your_array)\nalert(gon.your_hash)\n```\n\n### AMD compatible version: `include_gon_amd`\n\nIf your site uses AMD modules you can use the `include_gon_amd` helper to\ninclude the variables and watch function as a module. Options are mostly\nthe same as for `include_gon`, except for `namespace_check`, which does\nnothing and `namespace`, which is used as the name of the defined module.\nThe end result will look somewhat like the following:\n\n```js\ndefine('yourNameSpace', [], function() {\n  var gon = {};\n  gon.yourVariable = yourValue;\n  // etc...\n\n  return gon;\n});\n```\n\nA (very) simplified usage example:\n\n`app/views/layouts/application.html.erb`\n\n```ruby\ninclude_gon_amd namespace: 'data'\n```\n\n`Some JavaScript module`\n\n```js\ndefine(['data'], function(data) {\n  alert(data.myVariable);\n});\n```\n\n## gon.watch - renew your data easily!\n\nYou can use gon for renewing your data without reloading pages and\nwriting long js functions! It's really great for some live values.\n\nSupports `gon.watch.rabl` and `gon.watch.jbuilder` usage.\n\n[Instruction](https://github.com/gazay/gon/wiki/Usage-gon-watch) for\nusage gon.watch.\n\n## Usage with Rabl\n\nYou can write your variables assign logic to templates with [Rabl](https://github.com/nesquena/rabl).\nThe way of writing Rabl templates is very clearly described in their repo.\n\nProfit of using Rabl with gon:\n\n  1. You can clean your controllers now!\n  2. Work with database objects and collections clearly and easyly\n  3. All power of Rabl\n  4. You can still be lazy and don't use common way to transfer data in js\n  5. And so on\n\n[Instruction](https://github.com/gazay/gon/wiki/Usage-with-rabl) for\nusage gon with Rabl.\n\n## Usage with Rabl-Rails\n`gon.rabl` works with [rabl-rails](https://github.com/ccocchi/rabl-rails). Learn to write RABL the rabl-rails way [here](https://github.com/ccocchi/rabl-rails).\n\nAdd gon and rabl-rails to your environment:\n```ruby\ngem 'gon'\ngem 'rabl-rails'\n```\nDefine a rabl template using rabl-rails syntax:\n```rabl\n#app/views/users/show.rabl\nobject :@user\nattributes :id, :name, :email, :location\n```\nCall gon.rabl in your controller\n\n```ruby\n#app/controllers/users_controller.rb\ndef show\n  @user = User.find(params[:id])\n  gon.rabl\nend\n```\n\n## Usage with Jbuilder\n\nUse gon with [Jbuilder](https://github.com/rails/jbuilder) as with [Rabl](https://guthub.com/nesquena/rabl):\n\n[Instruction](https://github.com/gazay/gon/wiki/Usage-with-jbuilder) for\nusage gon with Jbuilder.\n\n## gon.global\n\nYou can use gon for sending your data to js from anywhere! It's really\ngreat for some init data.\n\n[Instruction](https://github.com/gazay/gon/wiki/Usage-gon-global) for\nusage gon.global.\n\n## Speed up Gon\n\nYou can use any [JSON Engine](https://github.com/intridea/multi_json#supported-json-engines) you want.\nGon uses `MultiJson` with autodetect mode, so all you need is just require your JSON library.\n\n## Contributors\n\n* @gazay\n* @takiy33\n\nSpecial thanks to @brainopia, @kossnocorp and @ai.\n\n## License\n\nThe MIT License\n\n## Security Contact\n\nTo report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.\n","funding_links":["https://tidelift.com/funding/github/rubygems/gon","https://tidelift.com/security"],"categories":["JavaScript","\u003e 1k ★","Ruby","View helpers","Misc"],"sub_categories":["JavaScript Tools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgazay%2Fgon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgazay%2Fgon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgazay%2Fgon/lists"}