Upgrading to V2
To migrate from version 0.x to 1.x, please see the wiki; https://github.com/gemgento/rails_script/wiki/v2-Migration
Add this line to your application's Gemfile:
gem 'rails_script', '~> 2.0'
And then execute:
After bundling you need to run the initial installation generator:
$ rails g rails_script:install
<%= include_rails_script %>
NOTE: Your JS files needed have been included before
include_rails_script. In other words, you still need
require_tree . line.
// * other app specific js require lines //= require rails_script //= require_tree .
# * other app specific js require lines #= require rails_script #= require_tree .
beforeAction is run before all controller action functions and
afterAction is run after all controller action functions. The before and after action functions have an optional argument
action which is a string containing the current action name. The example below would print 'before ACTION action' and 'after ACTION action' for each
Users controller action.
In this example we extracted the rollover action into a new function. Doing so will make the class cleaner and easier to maintain as the application grows. Once again note the
return this in the constructor.
Any functions that need to be accessible in the global scope should be defined in
global.js.coffee using the
App namespace. Below is an example of one of our favorite functions that we use to submit a form using AJAX as a JSON request.
Now you can access this function from anywhere in the application by just calling
Utility is a class that will be used to create similar functionality in many areas of the application. A good example of this is a Modal, which could appear multiple times on the same page. So, let's encapsulate this functionality in a highly reusable class.
First, generate the
$ rails g rails_script:utility Modal
This will create the following in
Let's add some basic functionality:
Now, here's how we use the utility from
Element is a class that describes the functionality of a one off element in the application. A Main Menu is a good example of this since there is usually only a single Main Menu.
First generate the
$ rails g rails_script:element MainMenu
This will create the following in
We can now add all the logic for the main menu in a separate class and call it on every page like so:
Inheritance is another key tool for reusability. Let's say our
Element.MainMenu opens and closes in the same way as the
Utility.Modal. Well then MainMenu should just extend Modal, this can be accomplished from the generator:
$ rails g rails:script MainMenu Modal
Inheritance from the generator can only come from a Utility class. Any class you wish to extend should be created as a Utility. The installer adds the line
//= require_tree ./utilities before loading tree to handle this. If you have a utility that extends a utility, then make sure the extended utility is loaded first by explicitly requiring it before
//= require_tree ./utilities.
$ rails g rails_script:controller Some::NewController
Since the above example includes a namespace, it would generate:
None of the pre-defined functions are necessary, you can remove the ones you don't need.
To generate a generic class that isn't a Utility, Element or Controller, just use the following:
$ rails g rails_script:class My::ClassName
Passing Rails Variables
Here's an example where
before_filter to pass the current user and their friends:
And here's how we print that data to the console on the
Since Turbolinks doesn't refresh the page and only replaces the body, event listeners defined on
document carry between page loads. To avoid these event listeners stacking, RailsScript will destroy all event listeners on
document that have a blank namespace, i.e.
$(window).on 'scroll', myHandler. If you need an event handler to persist between page changes, then define a namespace, i.e.
$(window).on 'scroll.namespace', myHandler.
Full page transitions are super easy with RailsScript and Turbolinks. Checkout the wiki for more information on how to add these to your RailsScript application, https://github.com/gemgento/rails_script/wiki/Turbolinks-Page-Transitions.
- Fork it ( https://github.com/[my-github-username]/rails_script/fork )
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Add some feature')
- Push to the branch (
git push origin my-new-feature)
- Create a new Pull Request