ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - peresleguine/pluggable_js: Page-specific javascript for Rails applications with the ability of passing data.
Page-specific javascript for Rails applications with the ability of passing data. - peresleguine/pluggable_js
Visit Site

GitHub - peresleguine/pluggable_js: Page-specific javascript for Rails applications with the ability of passing data.

GitHub - peresleguine/pluggable_js: Page-specific javascript for Rails applications with the ability of passing data.

PluggableJs

Build Status Gem Version

This gem provides simple functionality of loading page specific javascript and allows to pass data from a controller. It requires only Rails >= 3.1 with asset pipeline enabled. Keep desired js code in controller related files as action based functions. They will be triggered only when matching controller and action parameters and when DOM is ready.

Installation

  1. Add gem 'pluggable_js', '~> 2.2.0' to Gemfile and run bundle command to install it
  2. Add <%= javascript_pluggable_tag %> helper to application layout file above the closing </body> tag

The place for the helper is important. Primarily it serves the DOM ready purpose and completely necessary if you decided to use turbolinks.

Usage

Simply define functions in your controller related file (e.g. posts.coffee) like so:

@['posts#index'] = (data) ->
  # your code goes here
@['posts#new'] = (data) ->
  # and here

You may pass data to javascript function using pluggable_js helper in rails controller (pjs is an alias method). See example below:

class PostsController < ApplicationController
  def index
    pluggable_js(
      string: 'string',
      integer: 1,
      boolean: true,
      array: [1, 2, 3],
      hash: { a: 1, b: 2, c: 3 },
      array_of_hashes: [{a: 1}, {b: 2}, {c: 3}]
    )
  end
end

If you feel like this logic doesn't belong to a controller, safely move it to a view. Then you can access data in posts.coffee:

@['posts#index'] = (data) ->
  console.log data.string
  console.log data.integer
  console.log data.boolean
  console.log data.array
  console.log data.hash
  console.log data.array_of_hashes

CoffeeScript used here just for the sake of simplicity. You may implement the same with plain JavaScript.

Config

Let's say you've created action search that renders index template. Most likely we still need to trigger @['posts#index'](data) function. In such situation you may create config/initializers/pluggable_js.rb and use pair actions config:

PluggableJs.config do |config|
  config.pair_actions = { 'search' => 'index' }
end

{ 'create' => 'new', 'update' => 'edit' } is a default REST configuration.

Upgrade

Development notes

To run the test suite:

bundle exec cucumber

Sublime Text Snippet

Go to Sublime Text > Preferences > Browse Packages... and save under User directory pjs.sublime-snippet with the following content:

<snippet>
  <content><![CDATA[
@['$1#$2'] = (data) ->
  $0
]]></content>
  <tabTrigger>pjs</tabTrigger>
  <scope>source.coffee</scope>
</snippet>

Thereafter pjs snippet will be available in coffeescript files.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory