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

Merge pull request #75 from kbparagua/feature/manual-hook-insertion

Do not automatically insert paloma hook
This commit is contained in:
Karl Bryan Paragua 2015-04-25 18:09:40 +08:00
commit b712bd3d2a
7 changed files with 57 additions and 54 deletions

View file

@ -45,10 +45,23 @@ That's it! Simply Sexy!
## Install ## Install
* Without bundler: `sudo gem install paloma`. 1. Without bundler: `sudo gem install paloma`.
* With bundler, add this to your Gemfile: `gem 'paloma'` 1. With bundler, add this to your Gemfile: `gem 'paloma'`
* Require `paloma` in your `application.js`: `//= require paloma` 1. Require `paloma` in your `application.js`: `//= require paloma`
1. In your layouts insert Paloma hook.
`application.html.erb`
```html
<html>
<head>
</head>
<body>
<%= yield %>
<%= insert_paloma_hook %>
</body>
</html>
```
## Controllers ## Controllers
@ -257,6 +270,12 @@ class UsersController < ApplicationController
end end
``` ```
## Hook
`insert_paloma_hook` is a helper method that you can use in your views to insert Paloma's HTML hook.
Inside this HTML hook is where the magic happens. This is the reason why Paloma can magically know what Javascript controller/action to execute. To further understand how Paloma works, you can inspect the HTML hook, by checking the generated HTML (*inspect element*) and locate the `div` element that has the class `js-paloma-hook`.
Ideally, you just need to call `insert_paloma_hook` in your layouts, since the layout will always be included in every rendered view. But if you are rendering a view without a layout, make sure to call `insert_paloma_hook` in that view.
## Turbolinks Support ## Turbolinks Support
@ -289,14 +308,7 @@ $(document).on('page:load', function(){
## Gotchas ## Gotchas
* Paloma will execute on all `render` calls, except for calls with the following formats: `js`, `json`, `xml`, and `file`. * Make sure that the rendered view has the paloma hook (*use `insert_paloma_hook`*) for Paloma to execute.
Example:
```ruby
render :json => {:x => 1} # Paloma will not execute`
render :partial => '/path/to/partial' # Paloma will execute
```
* It will cause conflicts if you have a controller and a module that has the same name. * It will cause conflicts if you have a controller and a module that has the same name.

View file

@ -12,7 +12,7 @@ module Paloma
prepend_view_path "#{Paloma.root}/app/views/" prepend_view_path "#{Paloma.root}/app/views/"
before_filter :track_paloma_request before_filter :track_paloma_request
after_filter :append_paloma_hook, :if => :not_redirect? helper_method :insert_paloma_hook
end end
end end
@ -107,61 +107,26 @@ module Paloma
# #
# Before rendering html reponses, # Call in your view to insert Paloma's html hook.
# this is exectued to append Paloma's html hook to the response.
# #
# The html hook contains the javascript code that # The html hook contains the javascript code that
# will execute the tracked Paloma requests. # will execute the tracked Paloma requests.
# #
def append_paloma_hook def insert_paloma_hook
return true if self.paloma.has_no_request? return true if self.paloma.has_no_request?
# Render the partial if it is present, otherwise do nothing.
begin
hook = view_context.render(
:partial => 'paloma/hook',
:locals => {:request => self.paloma.request})
rescue ActionView::MissingTemplate
return true
end
before_body_end_index = response_body[0].rindex('</body>') hook = view_context.render(
:partial => 'paloma/hook',
# Append the hook after the body tag if it is present. :locals => {:request => self.paloma.request})
if before_body_end_index.present?
before_body = response_body[0][0, before_body_end_index].html_safe
after_body = response_body[0][before_body_end_index..-1].html_safe
response.body = before_body + hook + after_body
else
# If body tag is not present, append hook in the response body
response.body += hook
end
self.paloma.clear_request self.paloma.clear_request
hook
end end
end end
def not_redirect?
self.status != 302
end
#
# Make sure not to execute paloma on the following response type
#
def render options = nil, extra_options = {}, &block
[:json, :js, :xml, :file].each do |format|
if options.has_key?(format)
self.paloma.clear_request
break
end
end if options.is_a?(Hash)
super
end
protected protected

View file

@ -52,3 +52,16 @@ Foos.prototype.otherAction = function(){};
var NotFoos = Paloma.controller('NotAdmin/Foos'); var NotFoos = Paloma.controller('NotAdmin/Foos');
NotFoos.prototype.show = function(){}; NotFoos.prototype.show = function(){};
NotFoos.prototype.otherAction = function(){}; NotFoos.prototype.otherAction = function(){};
$(document).ready(function(){
$('#js-ajax-link').on('click', function(e){
e.preventDefault();
$.get($(this).prop('href'), function(response){
$('#js-ajax-response').html(response);
Paloma.executeHook();
Paloma.engine.start();
});
});
});

View file

@ -40,6 +40,11 @@ class MainController < ApplicationController
end end
def ajax
render :ajax, :layout => false
end
@ -63,7 +68,7 @@ class MainController < ApplicationController
def file_response def file_response
render :file => "#{Rails.root}/Gemfile" render :file => "#{Rails.root}/Gemfile", :layout => false
end end
end end

View file

@ -19,12 +19,17 @@
<li><%= link_to 'Main#basic_params', basic_params_main_index_path %></li> <li><%= link_to 'Main#basic_params', basic_params_main_index_path %></li>
<li><%= link_to 'Main#xml_response', xml_response_main_index_path %></li> <li><%= link_to 'Main#xml_response', xml_response_main_index_path %></li>
<li><%= link_to 'Main#file_response', file_response_main_index_path %></li> <li><%= link_to 'Main#file_response', file_response_main_index_path %></li>
<li><%= link_to 'Main#ajax', ajax_main_index_path, :id => 'js-ajax-link' %></li>
</ul> </ul>
</div> </div>
<hr/> <hr/>
<div id='js-ajax-response'>
</div>
<%= yield %> <%= yield %>
<%= insert_paloma_hook %>
</body> </body>
</html> </html>

View file

@ -0,0 +1,2 @@
<h1>Main#AJAX</h1>
<%= insert_paloma_hook %>

View file

@ -12,6 +12,7 @@ TestApp::Application.routes.draw do
get :js_response get :js_response
get :xml_response get :xml_response
get :file_response get :file_response
get :ajax
end end
end end