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

206 lines
5.1 KiB
Markdown
Raw Normal View History

2013-10-12 08:48:18 -04:00
# Paloma
2013-10-12 10:20:10 -04:00
**This README is for Paloma 3.
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 09:12:10 -04:00
Paloma (version 3) is almost a complete rewrite of the old version.
2013-10-12 08:48:18 -04:00
It is now simpler and it also gives more flexibility to the developers. Simplicity and flexibility are achieved by replacing the old callback thingy paradigm by a combination of `Router` and `Controller` components.
All the generator shits are also gone. So developers need not to follow specific folder structure or file name. And since there's no generated files or whatsoever, you can now code in vanilla javascript or **coffescript**! Yay!
2013-10-12 08:55:14 -04:00
**Basically, Paloma now provides a Controller for your javascript!**
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
// Executes when Rails User#new is rendered.
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 09:24:40 -04:00
@user = User.new
2012-12-19 02:56:52 -05:00
end
end
```
That's it! Simply Sexy!
Minimum Requirements
-
* 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
2012-12-19 02:56:52 -05:00
Without bundler:
```
sudo gem install paloma
```
2012-12-19 02:56:52 -05:00
With bundler, add this to your Gemfile:
```
2012-12-19 20:49:35 -05:00
gem 'paloma'
2012-12-19 02:56:52 -05:00
```
2012-12-19 20:49:35 -05:00
Require `paloma` in your `application.js`:
```
//= require paloma
```
2013-10-12 09:05:06 -04:00
2013-10-12 09:12:10 -04:00
## Router
2013-10-12 09:05:06 -04:00
Router is responsible for mapping Rails controller/action to its equivalent Paloma controller/action.
By default all Rails controller/action will be mapped with a Paloma controller/action with the same resource name (controller name without the `Controller` suffix).
Example:
* Response from `UsersController#new` will be mapped to `Users` Paloma controller and execute its `new` method.
2013-10-12 09:24:40 -04:00
### Changing Controller
2013-10-12 09:05:06 -04:00
2013-10-12 09:12:10 -04:00
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
// it will be mapped to AdminUsers.
Paloma.router.resource('Users', {controller: 'AdminUsers'});
```
### Redirecting
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
2013-10-12 09:41:44 -04:00
// `Registrations#signUp`.
2013-10-12 09:12:10 -04:00
Paloma.router.redirect('Users#new', {to: 'Registrations#signUp');
```
2013-10-12 09:05:06 -04:00
2013-10-12 09:24:40 -04:00
## Controller
2013-10-12 09:41:44 -04:00
Controller handles responses from Rails. A new controller instance is created by Paloma for every Rails controller/action that is executed.
2013-10-12 09:24:40 -04:00
### Creating Controller
A Controller is created or accessed (if already existing) using:
```javascript
Paloma.controller('ControllerName');
```
It returns the constructor of your controller. It is just a normal constructor so you can do your OOP stuff.
### Creating actions
Add instance methods to you Controller constructor to handle actions.
```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 09:36:38 -04:00
You can also pass parameters to Paloma 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 09:36:38 -04:00
## Preventing Paloma
2013-03-02 11:15:44 -05:00
2013-10-12 09:36:38 -04:00
If you want to prevent Paloma from executing in a certain Rails controller action you can do it by passing `false` to `js` command.
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 09:36:38 -04:00
Chains are created after a redirect action. The chain will continue to increase its length until a render action is detected.
2012-12-19 05:36:54 -05:00
**Example:**
```ruby
def first_action
redirect_to second_action_path
end
def second_action
redirect_to third_action_path
end
def third_action
render :template => 'third_action_view'
end
```
2013-10-12 09:36:38 -04:00
A request for `first_action` will lead to 2 redirects until it reaches the `third_action` and renders a result on the browser. When the `third_action` renders its response, Paloma will process all the request starting from `first_action` up to `third_action`.
2012-12-19 05:36:54 -05:00
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?
Again, Paloma is now flexible and doesn't force developers to follow specific folder structure.
You have the freedom to create controllers and routes anywhere in your javascript code.
Personally, I prefer having a `routes.js` file to contain all the route declaration, and a javascript file for each controller.