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:
commit
b712bd3d2a
7 changed files with 57 additions and 54 deletions
34
README.md
34
README.md
|
@ -45,10 +45,23 @@ That's it! Simply Sexy!
|
|||
|
||||
## Install
|
||||
|
||||
* Without bundler: `sudo gem install paloma`.
|
||||
* With bundler, add this to your Gemfile: `gem 'paloma'`
|
||||
* Require `paloma` in your `application.js`: `//= require paloma`
|
||||
1. Without bundler: `sudo gem install paloma`.
|
||||
1. With bundler, add this to your Gemfile: `gem '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
|
||||
|
||||
|
@ -257,6 +270,12 @@ class UsersController < ApplicationController
|
|||
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
|
||||
|
||||
|
@ -289,14 +308,7 @@ $(document).on('page:load', function(){
|
|||
|
||||
## Gotchas
|
||||
|
||||
* Paloma will execute on all `render` calls, except for calls with the following formats: `js`, `json`, `xml`, and `file`.
|
||||
|
||||
Example:
|
||||
|
||||
```ruby
|
||||
render :json => {:x => 1} # Paloma will not execute`
|
||||
render :partial => '/path/to/partial' # Paloma will execute
|
||||
```
|
||||
* Make sure that the rendered view has the paloma hook (*use `insert_paloma_hook`*) for Paloma to execute.
|
||||
|
||||
* It will cause conflicts if you have a controller and a module that has the same name.
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ module Paloma
|
|||
prepend_view_path "#{Paloma.root}/app/views/"
|
||||
|
||||
before_filter :track_paloma_request
|
||||
after_filter :append_paloma_hook, :if => :not_redirect?
|
||||
helper_method :insert_paloma_hook
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -107,61 +107,26 @@ module Paloma
|
|||
|
||||
|
||||
#
|
||||
# Before rendering html reponses,
|
||||
# this is exectued to append Paloma's html hook to the response.
|
||||
# Call in your view to insert Paloma's html hook.
|
||||
#
|
||||
# The html hook contains the javascript code that
|
||||
# will execute the tracked Paloma requests.
|
||||
#
|
||||
def append_paloma_hook
|
||||
def insert_paloma_hook
|
||||
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>')
|
||||
|
||||
# Append the hook after the body tag if it is present.
|
||||
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
|
||||
hook
|
||||
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
|
||||
|
||||
|
|
|
@ -52,3 +52,16 @@ Foos.prototype.otherAction = function(){};
|
|||
var NotFoos = Paloma.controller('NotAdmin/Foos');
|
||||
NotFoos.prototype.show = 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -40,6 +40,11 @@ class MainController < ApplicationController
|
|||
end
|
||||
|
||||
|
||||
def ajax
|
||||
render :ajax, :layout => false
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -63,7 +68,7 @@ class MainController < ApplicationController
|
|||
|
||||
|
||||
def file_response
|
||||
render :file => "#{Rails.root}/Gemfile"
|
||||
render :file => "#{Rails.root}/Gemfile", :layout => false
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -19,12 +19,17 @@
|
|||
<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#file_response', file_response_main_index_path %></li>
|
||||
<li><%= link_to 'Main#ajax', ajax_main_index_path, :id => 'js-ajax-link' %></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<hr/>
|
||||
|
||||
<div id='js-ajax-response'>
|
||||
</div>
|
||||
|
||||
<%= yield %>
|
||||
<%= insert_paloma_hook %>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
|
2
test_app/app/views/main/ajax.html.erb
Normal file
2
test_app/app/views/main/ajax.html.erb
Normal file
|
@ -0,0 +1,2 @@
|
|||
<h1>Main#AJAX</h1>
|
||||
<%= insert_paloma_hook %>
|
|
@ -12,6 +12,7 @@ TestApp::Application.routes.draw do
|
|||
get :js_response
|
||||
get :xml_response
|
||||
get :file_response
|
||||
get :ajax
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue