{"id":14956039,"url":"https://github.com/tranquangvu/raicon","last_synced_at":"2025-08-22T16:08:48.069Z","repository":{"id":51671705,"uuid":"221673576","full_name":"tranquangvu/raicon","owner":"tranquangvu","description":"Page specific Javascript for Ruby On Rails application use Webpack to manage assets","archived":false,"fork":false,"pushed_at":"2022-12-04T20:24:18.000Z","size":91,"stargazers_count":13,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-09T13:59:14.929Z","etag":null,"topics":["javascript","page-specific-js","rails","ruby-on-rails","webpack"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/tranquangvu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-14T10:43:16.000Z","updated_at":"2025-05-15T05:36:22.000Z","dependencies_parsed_at":"2023-01-23T19:45:41.375Z","dependency_job_id":null,"html_url":"https://github.com/tranquangvu/raicon","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tranquangvu/raicon","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranquangvu%2Fraicon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranquangvu%2Fraicon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranquangvu%2Fraicon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranquangvu%2Fraicon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tranquangvu","download_url":"https://codeload.github.com/tranquangvu/raicon/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tranquangvu%2Fraicon/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271665141,"owners_count":24799302,"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","status":"online","status_checked_at":"2025-08-22T02:00:08.480Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["javascript","page-specific-js","rails","ruby-on-rails","webpack"],"created_at":"2024-09-24T13:12:13.449Z","updated_at":"2025-08-22T16:08:48.037Z","avatar_url":"https://github.com/tranquangvu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raicon\nPage specific Javascript for Ruby On Rails application use Webpack to manage assets.\n\n## Installation\n```\n  // npm\n  npm install --save raicon\n\n  // yarn\n  yarn add raicon\n```\n\n## Usage\nRaicon runs your JS code after DOM is ready (support turbolinks), don't need to add `$(document).ready` or `document.addEventListener('turbolinks:load', () =\u003e {})`.\n\n### Add raicon data attributes\n  - Define helper method in `helpers/application_helper.rb`:\n\n    ```\n      // With *.erb\n      def raicon_data_attributes\n        \"data-raicon-controller=#{controller_path} \" \\\n          \"data-raicon-action=#{action_name}\"\n      end\n\n      // With *.slim\n      def raicon_data_attributes\n        {\n          data: {\n            'raicon-controller': controller_path,\n            'raicon-action': action_name\n          }\n        }\n      end\n    ```\n\n  - Add data attributes defined from `raicon_data_attributes` method to `body` tag:\n\n    ```\n      // With *.erb\n      \u003cbody \u003c%= raicon_data_attributes %\u003e\u003e\n        \u003c%= yield %\u003e\n      \u003c/body\u003e\n\n      // With *.slim\n      body *raicon_data_attributes\n        == yield\n    ```\n\n### Register handler class for target controller\n  - To run JS on a certain page, you can register a handler like this:\n\n    ```\n      import Raicon from 'raicon';\n\n      Raicon.register(targetController, hanlerClass, hasTurbolinks = true);\n\n      // Arguments:\n      //  - targetController: string - camelcase transformation of `controller_path` value of the controller from rails.\n      //  - hanlerClass: class - class includes methods (method's name is camelcase transformation of `action_name` value of the controller from rails) to run JS code for specific page.\n      //  - hasTurbolinks: boolean (default is true) - check if we use turbolinks or not\n    ```\n\n  - Controller path and action name camelcase transformation example:\n    ```\n      // Controller path transformation\n      controller: 'app/controllers/my_posts_controller.rb' -\u003e controller_path: 'my_posts' -\u003e targetController: 'myPosts'\n      controller: 'app/controllers/my_admin/my_posts_controller.rb' -\u003e controller_path: 'my_admin/my_posts' -\u003e targetController: 'myAdmin/myPosts'\n\n      // Action name transformation\n      action: 'posts' -\u003e methodName: 'posts'\n      action: 'favorite_posts' -\u003e methodName: 'favoritePosts'\n    ```\n\n\n  - Example:\n\n    Rails controller in `app/controllers/my_posts_controller.rb`:\n\n    ```\n      class MyPostsController \u003c ApplicationController\n        def index; end\n\n        def favorite_posts; end\n\n        def new; end\n\n        def create; end\n\n        def edit; end\n\n        def update; end\n\n        def destroy; end\n      end\n    ```\n\n    JS Raicon controller:\n\n    ```\n      import Raicon from 'raicon';\n\n      class MyPostsController {\n        beforeEach() {\n          console.log('Run after DOM ready in all pages');\n        }\n\n        index() {\n          console.log('Run after DOM ready in page rendered by app/views/my_posts/index.html.erb');\n        }\n\n        favoritePosts() {\n          console.log('Run after DOM ready in page rendered by app/views/my_posts/favorite_posts.html.erb');\n        }\n\n        new() {\n          console.log('Run after DOM ready in page rendered by app/views/my_posts/new.html.erb');\n          this.initForm();\n        }\n\n        edit() {\n          console.log('Run after DOM ready in page rendered by app/views/my_posts/edit.html.erb');\n          this.initForm();\n        }\n\n        initForm() {\n          console.log('Init form');\n        }\n      }\n\n      Raicon.register('myPosts', MyPostsController);\n    ```\n\n  - Reuse method from handler class:\n\n    ```\n      window.myPostsRc = Raicon.register('myPosts', MyPostsController);\n\n      // Reuse method in handler\n      const myPostsController = window.myPostsRc.getHandler();\n      myPostsController.initForm();\n    ```\n\n### Events\nRaicon support `:before` and `:after` events for every action in controller. The name of event follow this pattern:\n\n```\n  'raicon:before:${targetController}#${methodName}'\n  'raicon:after:${targetController}#${methodName}'\n```\n\nFor above raicon controller we have these event listeners:\n\n```\n  document.addEventListener('raicon:before:myPosts#index', () =\u003e {});\n  document.addEventListener('raicon:after:myPosts#index', () =\u003e {});\n\n  document.addEventListener('raicon:before:myPosts#favoritePosts', () =\u003e {});\n  document.addEventListener('raicon:after:myPosts#favoritePosts', () =\u003e {});\n\n  document.addEventListener('raicon:before:myPosts#new', () =\u003e {});\n  document.addEventListener('raicon:after:myPosts#new', () =\u003e {});\n\n  document.addEventListener('raicon:before:myPosts#edit', () =\u003e {});\n  document.addEventListener('raicon:after:myPosts#edit', () =\u003e {});\n```\n\n## License\nThis package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftranquangvu%2Fraicon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftranquangvu%2Fraicon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftranquangvu%2Fraicon/lists"}