npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

raicon

v1.0.4

Published

Page specific Javascript for Ruby On Rails with Webpack inside

Downloads

254

Readme

Raicon

Page specific Javascript for Ruby On Rails application use Webpack to manage assets.

Installation

  // npm
  npm install --save raicon

  // yarn
  yarn add raicon

Usage

Raicon runs your JS code after DOM is ready (support turbolinks), don't need to add $(document).ready or document.addEventListener('turbolinks:load', () => {}).

Add raicon data attributes

  • Define helper method in helpers/application_helper.rb:

      // With *.erb
      def raicon_data_attributes
        "data-raicon-controller=#{controller_path} " \
          "data-raicon-action=#{action_name}"
      end
    
      // With *.slim
      def raicon_data_attributes
        {
          data: {
            'raicon-controller': controller_path,
            'raicon-action': action_name
          }
        }
      end
  • Add data attributes defined from raicon_data_attributes method to body tag:

      // With *.erb
      <body <%= raicon_data_attributes %>>
        <%= yield %>
      </body>
    
      // With *.slim
      body *raicon_data_attributes
        == yield

Register handler class for target controller

  • To run JS on a certain page, you can register a handler like this:

      import Raicon from 'raicon';
    
      Raicon.register(targetController, hanlerClass, hasTurbolinks = true);
    
      // Arguments:
      //  - targetController: string - camelcase transformation of `controller_path` value of the controller from rails.
      //  - 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.
      //  - hasTurbolinks: boolean (default is true) - check if we use turbolinks or not
  • Controller path and action name camelcase transformation example:

      // Controller path transformation
      controller: 'app/controllers/my_posts_controller.rb' -> controller_path: 'my_posts' -> targetController: 'myPosts'
      controller: 'app/controllers/my_admin/my_posts_controller.rb' -> controller_path: 'my_admin/my_posts' -> targetController: 'myAdmin/myPosts'
    
      // Action name transformation
      action: 'posts' -> methodName: 'posts'
      action: 'favorite_posts' -> methodName: 'favoritePosts'
  • Example:

    Rails controller in app/controllers/my_posts_controller.rb:

      class MyPostsController < ApplicationController
        def index; end
    
        def favorite_posts; end
    
        def new; end
    
        def create; end
    
        def edit; end
    
        def update; end
    
        def destroy; end
      end

    JS Raicon controller:

      import Raicon from 'raicon';
    
      class MyPostsController {
        beforeEach() {
          console.log('Run after DOM ready in all pages');
        }
    
        index() {
          console.log('Run after DOM ready in page rendered by app/views/my_posts/index.html.erb');
        }
    
        favoritePosts() {
          console.log('Run after DOM ready in page rendered by app/views/my_posts/favorite_posts.html.erb');
        }
    
        new() {
          console.log('Run after DOM ready in page rendered by app/views/my_posts/new.html.erb');
          this.initForm();
        }
    
        edit() {
          console.log('Run after DOM ready in page rendered by app/views/my_posts/edit.html.erb');
          this.initForm();
        }
    
        initForm() {
          console.log('Init form');
        }
      }
    
      Raicon.register('myPosts', MyPostsController);
  • Reuse method from handler class:

      window.myPostsRc = Raicon.register('myPosts', MyPostsController);
    
      // Reuse method in handler
      const myPostsController = window.myPostsRc.getHandler();
      myPostsController.initForm();

Events

Raicon support :before and :after events for every action in controller. The name of event follow this pattern:

  'raicon:before:${targetController}#${methodName}'
  'raicon:after:${targetController}#${methodName}'

For above raicon controller we have these event listeners:

  document.addEventListener('raicon:before:myPosts#index', () => {});
  document.addEventListener('raicon:after:myPosts#index', () => {});

  document.addEventListener('raicon:before:myPosts#favoritePosts', () => {});
  document.addEventListener('raicon:after:myPosts#favoritePosts', () => {});

  document.addEventListener('raicon:before:myPosts#new', () => {});
  document.addEventListener('raicon:after:myPosts#new', () => {});

  document.addEventListener('raicon:before:myPosts#edit', () => {});
  document.addEventListener('raicon:after:myPosts#edit', () => {});

License

This package is available as open source under the terms of the MIT License.