1
0
Fork 0
mirror of https://github.com/kbparagua/paloma synced 2023-03-27 23:21:17 -04:00
paloma/README.md

204 lines
6.5 KiB
Markdown
Raw Normal View History

2013-10-12 08:48:18 -04:00
# Paloma
2013-10-12 21:59:43 -04:00
**This README is for Paloma 3 only.
2013-10-12 10:20:10 -04:00
For version 2 README please go [here](https://github.com/kbparagua/paloma/blob/2.0/README.md).**
2013-10-12 08:48:18 -04:00
## What's New?
2013-10-12 21:59:43 -04:00
Paloma 3 is almost a complete rewrite of the old version.
2013-10-12 08:48:18 -04:00
2013-10-12 21:59:43 -04:00
It is now simpler and more flexible. The old callback thingy paradigm is replaced by a `Router` and `Controller` layer for better abstraction. Generators are also removed, so programmers need not to follow specific directory structure, unlike in the old versions.
2013-10-12 08:48:18 -04:00
2013-10-12 21:59:43 -04:00
Previously, there are generators that create Paloma files, and these files are written in vanilla javascript. Because of that there are some users who are requesting for coffeescript setup. Now since there are no generated files programmers can write their code either by using vanilla javascript or **coffeescript**. Yay!
### Controller and Router
2013-10-12 22:52:21 -04:00
The new paradigm is pattered after Rails Controller and Routes, so it is easier to grasp than the old callback paradigm. Basically, you have a Paloma Controller that is responsible for processing requests made from Rails Controller. While the Router is responsible for telling what Paloma Controller handles what Rails Controller, or what Paloma Controller's action handles what Rails Controller's action.
2013-10-12 08:48:18 -04:00
2013-10-12 22:08:49 -04:00
### How about Model and View?
2013-10-12 22:52:21 -04:00
It is tempting to convert Paloma 3 to a full-blown MVC or MVP (or whatever) framework. But I've decided to keep it simple and just provide a Controller component as way to execute a specific javascript code per Rails Controller action and give developers freedom on how to handle each action. So you can still have your own Model and View components and just use them in your Paloma Controllers, since a controller is just a middle-man.
2013-10-12 22:08:49 -04:00
2013-10-12 08:48:18 -04:00
## Advantages
* Choose what specific javascript code to run per page.
2013-01-25 22:08:50 -05:00
* Easily make ruby variables available on your javascript files.
2012-12-19 02:56:52 -05:00
2013-10-12 08:55:14 -04:00
2013-10-12 08:48:18 -04:00
## Quick Example
2012-12-19 02:56:52 -05:00
2013-10-12 08:55:14 -04:00
Paloma controller.
2012-12-19 02:56:52 -05:00
```javascript
2013-10-12 09:05:06 -04:00
var UsersController = Paloma.controller('Users');
2013-10-12 08:55:14 -04:00
2013-10-12 22:39:27 -04:00
// Executes when Rails User#new is executed.
2013-10-12 09:05:06 -04:00
UsersController.prototype.new = function(){
2013-10-12 08:55:14 -04:00
alert('Hello Sexy User!' );
};
2012-12-19 02:56:52 -05:00
```
The Rails controller `app/controllers/users_controller.rb`:
```ruby
def UsersController < ApplicationController
def new
2013-10-12 22:52:21 -04:00
# a Paloma request will automatically be created.
2013-10-12 09:24:40 -04:00
@user = User.new
2012-12-19 02:56:52 -05:00
end
end
```
That's it! Simply Sexy!
2013-10-12 22:39:27 -04:00
## Minimum Requirements
2012-12-19 02:56:52 -05:00
* jQuery 1.7 or higher
* Rails 3.1 or higher
2012-12-18 04:47:24 -05:00
2013-10-12 08:55:14 -04:00
## Install
2013-10-12 22:39:27 -04:00
* Without bundler: `sudo gem install paloma`.
* With bundler, add this to your Gemfile: `gem 'paloma'`
* Require `paloma` in your `application.js`: `//= require paloma`
2013-10-12 09:12:10 -04:00
2013-10-12 22:39:27 -04:00
## Controllers
2013-10-12 09:05:06 -04:00
2013-10-12 22:52:21 -04:00
Controllers are just classes that handle requests made by Rails Controllers. Each Rails Controller's action will be mapped to a specific Paloma Controller's action.
2013-10-12 09:24:40 -04:00
2013-10-12 22:39:27 -04:00
### Creating a Controller
2013-10-12 09:24:40 -04:00
2013-10-12 22:39:27 -04:00
A Controller constructor is created or accessed, if it already exists, using `Paloma.controller()` method.
2013-10-12 09:24:40 -04:00
```javascript
2013-10-12 22:39:27 -04:00
var MyController = Paloma.controller('MyController');
2013-10-12 09:24:40 -04:00
```
2013-10-12 22:39:27 -04:00
It will return the constructor function of your controller.
2013-10-12 09:24:40 -04:00
2013-10-12 22:39:27 -04:00
### Handling Actions
2013-10-12 09:24:40 -04:00
2013-10-12 22:39:27 -04:00
Every time a request to Paloma is made (A Rails Controller action is executed), an instance of a Paloma controller is created and a method responsible for the request will be invoked.
2013-10-12 09:24:40 -04:00
```javascript
var ArticlesController = Paloma.controller('ArticlesController');
ArticlesController.prototype.new = function(){
// Handle new articles
};
ArticlesController.prototype.edit = function(){
// Handle edit articles
};
```
2013-10-12 09:05:06 -04:00
2013-10-12 09:36:38 -04:00
## Passing Parameters
2013-01-18 21:46:26 -05:00
2013-10-12 22:39:27 -04:00
You can also pass parameters to a Paloma Controller by calling `js` **before** render in your Rails controller. You can access the parameters on your Paloma Controller using `this.params` object.
2012-12-19 05:28:03 -05:00
**Example:**
`users_controller.rb`
```ruby
def destroy
user = User.find params[:id]
user.destroy
2013-10-12 09:36:38 -04:00
js :id => user.id
2012-12-19 05:28:03 -05:00
end
```
2013-10-12 09:36:38 -04:00
Paloma controller.
2013-03-02 11:07:45 -05:00
```javascript
2013-10-12 09:36:38 -04:00
var UsersController = Paloma.controller('Users');
2013-03-02 11:07:45 -05:00
2013-10-12 09:36:38 -04:00
UsersController.prototype.destroy = function(){
alert('User ' + this.params['id'] + ' is deleted.');
2013-03-02 11:07:45 -05:00
};
```
2013-03-02 11:02:13 -05:00
2013-10-12 22:39:27 -04:00
## Preventing Paloma Execution
2013-03-02 11:15:44 -05:00
2013-10-12 22:39:27 -04:00
If you want to Paloma not to execute in a specific Rails Controller action you need to pass `false` as the Paloma parameter.
2013-03-02 11:15:44 -05:00
2013-10-12 09:36:38 -04:00
```ruby
def edit
@user = User.find params[:id]
js false
end
2013-03-02 11:36:36 -05:00
```
2013-03-02 11:33:26 -05:00
2013-10-12 09:36:38 -04:00
## Execution Chains
2013-03-02 11:33:26 -05:00
2013-10-12 22:39:27 -04:00
A Paloma request is created every time a Rails Controller action is executed. All requests will be recorded until a render action is triggered, and all Paloma requests will be processed following FIFO (First In, First Out) method.
2012-12-19 05:36:54 -05:00
**Example:**
```ruby
def first_action
2013-10-12 22:39:27 -04:00
# Paloma request will be created
2012-12-19 05:36:54 -05:00
redirect_to second_action_path
end
def second_action
2013-10-12 22:39:27 -04:00
# Paloma request will be created
2012-12-19 05:36:54 -05:00
redirect_to third_action_path
end
def third_action
2013-10-12 22:39:27 -04:00
# Paloma request will be created
2012-12-19 05:36:54 -05:00
render :template => 'third_action_view'
end
```
2013-10-12 22:39:27 -04:00
When the `third_action` renders its response, Paloma will process all the request starting from `first_action` up to `third_action`. So, Paloma Controller actions responsible for those 3 Rails actions will be executed.
## Router
Router is responsible for mapping Rails Controller/action to its equivalent Paloma Controller/action.
By default all Rails Controller will be mapped with a Paloma Controller with the same resource name (controller name without the `Controller` suffix).
Example:
* Requests from `UsersController#new` will be mapped to Paloma Controller named `Users` with its `new` method.
If you have no problem with the default behavior, you don't need to write Router stuff in your code.
### Changing Controller
If you want to use a different Paloma Controller for a specific Rails Controller, you can do the following:
```javascript
// Instead of mapping Rails UsersController to Paloma Users Controller
// it will be mapped to AdminUsers.
Paloma.router.resource('Users', {controller: 'AdminUsers'});
```
### Redirecting
2012-12-19 05:36:54 -05:00
2013-10-12 22:39:27 -04:00
You can also redirect an action if you want it to be handled by a different method.
```javascript
// Instead of executing Paloma's `Users#new` it will execute
// `Registrations#signUp`.
Paloma.router.redirect('Users#new', {to: 'Registrations#signUp');
```
2012-12-19 05:39:30 -05:00
2013-10-12 09:36:38 -04:00
## Gotchas
2012-12-19 05:39:30 -05:00
2013-10-12 09:36:38 -04:00
* Paloma will not execute if the response is `js`, `json`, `xml` or any other format except `html`.
2012-12-21 15:10:43 -05:00
2013-10-12 09:36:38 -04:00
For example: `render "something.js.erb"`
2013-10-12 10:09:02 -04:00
## Where to put code?
2013-10-12 22:39:27 -04:00
Again, Paloma is now flexible and doesn't force developers to follow specific directory structure.
You have the freedom to create controllers and routes anywhere in your application.
2013-10-12 10:09:02 -04:00
2013-10-12 22:39:27 -04:00
Personally, I prefer having a `routes.js` file to contain all the route declarations, and a javascript file for each controller.