1
0
Fork 0
mirror of https://github.com/kbparagua/paloma synced 2023-03-27 23:21:17 -04:00
page-specific javascript for Rails done right
Find a file
Karl Bryan Paragua 8530fc2aaa Update README.md
2012-12-19 18:28:03 +08:00
app/views/paloma callback tests 2012-12-19 14:59:38 +08:00
lib Merge branch 'master' of github.com:kbparagua/paloma 2012-12-19 18:04:58 +08:00
spec callback tests 2012-12-19 14:59:38 +08:00
.gitignore updated .gitignore. working sample_app 2012-12-18 12:26:30 +08:00
Gemfile creating test app 2012-12-17 18:50:23 +08:00
Gemfile.lock starting with tests 2012-12-18 18:39:33 +08:00
paloma.gemspec starting with tests 2012-12-18 18:39:33 +08:00
Rakefile creating test app 2012-12-17 18:50:23 +08:00
README.md Update README.md 2012-12-19 18:28:03 +08:00

Paloma

Paloma provides a sexy way to organize javascript files using Rails' asset pipeline. It adds the capability to execute specific javascript code after rendering the controller's response.

Advantages

  • Javascript files are organized per controller just like app/views folder of Rails.
  • Javascript file per controller's action.
  • The ability to choose what specific javascript code to run on a specific action.

Quick Example

The javascript callback file paloma/users/new.js:

Paloma.callbacks['users/new'] = function(params){
    // This will only run after executing users/new action
    alert('Hello New Sexy User');
};

The Rails controller app/controllers/users_controller.rb:

def UsersController < ApplicationController
    def new
        @user = User.new
        # No special function to call, the javascript callback will be executed automatically
    end
end

That's it! Simply Sexy!

Minimum Requirements

  • jQuery 1.7 or higher
  • Rails 3.1 or higher

Install

Without bundler:

sudo gem install paloma

With bundler, add this to your Gemfile:

gem paloma

Setup

On setup, the paloma folder will be generated in app/assets/javascripts/ containing its required files. Run:

rails g paloma:setup

Directory Structure

paloma folder contains the javascript callbacks.

  • paloma
    • [controllers]
      • [action].js
      • [other_action].js
    • [other_controllers]
      • [action].js
      • [other_action].js
      • [more_action].js

Generators

  1. Generate a controller folder containing its required files:
rails g paloma:add [controllers]
**Example:**
```
rails g paloma:add users
```

**Generates:**
* /paloma
    * /users
  1. Generate a callback file for a controller's action:
rails g paloma:add [controllers]/[action]
**Example:**
```
rails g paloma:add users/new
```

**Generates:**
* /paloma
    * /users
        * new.js

Note: You can directly run rails g paloma:add [controllers]/[action] even the controller folder is not yet existing on paloma folder. It will be created automatically.

Advanced Callbacks

By default Paloma will execute the callback that matches the response's current controller and action if it finds one. For instance if the current response is from the new action of the Users controller, then Paloma will execute the callback named users/new.

You can manipulate callback behavior by using the js_callback command before the render or redirect_to command in your controllers.

  1. Preventing the Callback to execute.

    def destroy
        user = User.find params[:id]
        user.destroy
    
        js_callback false
    end
    

    [controllers]/destroy callback will not be executed.

  2. Using other action's callback from the same controller.

    def edit
        @user = User.find params[:id]
        js_callback :new
    end
    

    This will execute [controller]/new callback instead of [controller]/edit.

  3. Using other action's callback from other controller.

    def index
        @users = User.all
        js_callback :controller => 'clients', :action => 'index'
    end
    

    This will execute clients/index callback instead of [controller]/index.

Passing Parameters

You can also pass parameters to the callback by passing a :params key to js_callback. The passed parameters will be available on the callback by the params object.

Example:

users_controller.rb

def destroy
    user = User.find params[:id]
    user.destroy
    
    js_callback :params => {:user_id => params[:id]}
end

/paloma/users/destroy.js

Paloma.callbacks['users/destroy'] = function(params){
    var id = params['user_id'];
    alert('User ' + id + ' deleted.');
};