Giter Site home page Giter Site logo

falm / js-namespace-rails Goto Github PK

View Code? Open in Web Editor NEW
57.0 5.0 1.0 63 KB

js-namespace-rails let you choose which javascript snippet can execute in rails assets pipeline

Ruby 70.13% JavaScript 10.29% Shell 0.35% CSS 2.08% HTML 17.16%
js-namespace-rails rails-asset-pipeline rails namespace assets

js-namespace-rails's Introduction

JsNamespaceRails Build Status Coverage Status Code Climate Dependency Status Gem Version

Rails's asset pipeline compiles all of js file into a single file which is executed on all pages. There has a problem, some time we want to execute selective code on specific page, but asset pipeline doesn't support. js-namespace-rails can handle this problem by using it's method to namespace and selectively execute certain javascript depending on which Rails controller action is active.

Installation

Add this line to your application's Gemfile:

gem 'js-namespace-rails'

Setup

Require js-namespace-rails file in application.js or other main file, notice js-namespace-rails has no dependency

//= require js-namespace-rails

Usage

Assume your project have articles_controller

class ArticlesController < ApplicationController
  def index
  end
end

And its corresponding js file app/assets/javascripts/articles.js.erb

then you just need to write below into the js file

// app/assets/javascripts/articles.js.erb
JsSpace.on('articles', {
  init: function(){
  	console.log('common logic of article in here');
  },
  index: function(){
  	console.log('logic of index action in here');
  }
});

Feature

Passing Parameters to js

class ArticlesController < ApplicationController
  def show
    @article = Article.find(params[:id])
    js author: @article.author
    # also you can passing an object
    js article: @article
  end
end
// app/assets/javascripts/articles.js.erb
JsSpace.on('articles', {
  show: function(){
    console.log(this.params.author); // get author from params
    console.log(this.params.article.title); // get title of article
  }
});

License

MIT License.

js-namespace-rails's People

Contributors

falm avatar jason-atyun avatar xiajian2019 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

xiajian2019

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.