Project

gistyle

0.01
No commit activity in last 3 years
No release in over 3 years
GIStyle is a Rails plug-in for DOM-based routing of Javascript, inspired from Paul Irish and Jason Garber.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

GIStyle

GIStyle (Garber-Irish Style) is a Rails plug-in for DOM-based routing of Javascript, inspired from Paul Irish and Jason Garber.

Usage

Install

Gemfile

...
gem 'gistyle'
...

app/assets/javascripts/application.js

...
//= require jquery
//= require jquery_ujs
//= require gistyle
//= require_tree .

APP.init = function() {
  console.log("application"); // global
};
...

Be sure to place require gistyle before require_tree ..

app/views/layouts/application.html.erb

...
<body data-controller="<%= controller_path %>" data-action="<%= action_name %>">
...

Example

JavaScript

app/assets/javascripts/home.js

...
APP.home = {
  init: function() {
    console.log("home controller wide");
  },
  index: function() {
    console.log("home#index");
  },
  new: function() {
    console.log("home#new");
  },
  _new_create: function() {
    console.log("home#new or home#create)");
  },
  _edit_update: function() {
    console.log("home#edit or home#update)");
  },
  _form: function() {
    console.log("Action with form (by default new, create, edit and update). See aliases (below) for more info.");
  },
  about: function() {
    console.log("home#about");
  },
  contact: function() {
    console.log("home#contact");
  }
}

CoffeeScript

app/assets/javascripts/admin/posts.js.coffee

APP['admin/posts'] = # supports namespace as wall
  index: () ->
    # blablabla
  show: () ->
    # blablabla
  new: () ->
    # blablabla
  edit: () ->
    # blablabla
  update: () ->
    # blablabla
    this.subroutine()
  subroutine () ->
    # blablabla

Aliases

It is possible to define aliases, i.e. actions that (also) trigger another action. This is useful to for example have an action _form that is triggered for all actions with a form (by default new, create, edit and update).

Aliases are defined by calling: GIStyle.alias(controller, action_from, action_to). It is also possible to define general aliases for all controllers by setting controller to undefined.

A few general aliases are included by default:

  • new, create, edit, update => _form
  • new, create => _new_create
  • edit, update => _new_update

Aliases can be cleared by calling either:

  • GIStyle.clear_aliases_for_controller(controller) or
  • GIStyle.clear_all_aliases().

Turbolinks Compatibility

It dosen't support turbolinks with version 1.x, please upgrade to 2.x if you want to make it work.