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

Merge pull request #48 from kbparagua/master

Master
This commit is contained in:
Karl Bryan Paragua 2014-06-07 19:50:08 +08:00
commit 7da1bf66e4
10 changed files with 97 additions and 26 deletions

View file

@ -1,5 +1,12 @@
# Changelog
## Version 4.1.0
* Support for Turbolinks.
* `Paloma.executeHook()` to manually run the hook from the DOM.
* Paloma hook will be appended using all `render` calls, except for calls that has the following keys `[:json, :js, :xml, :file]`.
* Restore `Paloma.engine.start()` to start processing queued request.
## Version 4.0.0
* https://github.com/kbparagua/paloma/issues/26 - Paloma requests are not saved on `session`.
* https://github.com/kbparagua/paloma/issues/26 - Chaining with redirect is removed.

View file

@ -276,12 +276,32 @@ class UsersController < ApplicationController
end
```
## Turbolinks Support
As of version `4.1.0`, Paloma is compatible with Turbolinks without additional setup.
### Execute Paloma when user hits `Back` or `Forward` button.
Paloma executes page-specific javascript by adding a `<script>` tag to the response body. Turbolinks, by default, executes any inline javascript in the response body when you visit a page, so the `<script>` tag appended by Paloma will automatically be executed. However, when Turbolinks restores a page from cache (*this happens when a user hits `Back` or `Forward` button in his browser*) any **inline javascript will not be executed** anymore. This is the intentional behavior of Turbolinks, and it is not a bug. If you want to execute Paloma again when Turbolinks restores a page, do something like this:
```js
$(document).on('page:restore', function(){
// Manually evaluates the appended script tag.
Paloma.executeHook();
});
```
## Gotchas
* Paloma will not execute if the response is `js`, `json`, `xml` or any other format except `html`.
* Paloma will execute on all `render` calls, except for calls with the following formats: `js`, `json`, `xml`, and `file`.
Example:
`render "something.js.erb"`
```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.

View file

@ -3,7 +3,6 @@
<div class="js-paloma-hook" data-id="<%= id %>">
<script type="text/javascript">
(function(){
// Do not continue if Paloma not found.
if (window['Paloma'] === undefined && window['console'] !== undefined){
console.warn("Paloma not found. Require it in your application.js.");
@ -17,13 +16,12 @@
var request = <%= request.to_json.html_safe %>;
$(document).ready(function(){
Paloma.engine.request(
request['resource'],
request['action'],
request['params']);
});
Paloma.engine.setRequest(
request['resource'],
request['action'],
request['params']);
$(document).ready(function(){ Paloma.engine.start(); });
})();
</script>
</div>

View file

@ -12,7 +12,7 @@ module Paloma
prepend_view_path "#{Paloma.root}/app/views/"
before_filter :track_paloma_request
after_filter :append_paloma_hook, :if => :html_is_rendered?
after_filter :append_paloma_hook, :if => :not_redirect?
end
end
@ -138,9 +138,8 @@ module Paloma
end
def html_is_rendered?
not_redirect = self.status != 302
[nil, 'text/html'].include?(response.content_type) && not_redirect
def not_redirect?
self.status != 302
end
@ -149,7 +148,10 @@ module Paloma
#
def render options = nil, extra_options = {}, &block
[:json, :js, :xml, :file].each do |format|
self.paloma.clear_request if options.has_key?(format)
if options.has_key?(format)
self.paloma.clear_request
break
end
end if options.is_a?(Hash)
super

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'paloma'
s.version = '4.0.0'
s.version = '4.1.0'
s.summary = "Provides an easy way to execute page-specific javascript for Rails."
s.description = "Page-specific javascript for Rails done right"
s.authors = ['Karl Paragua']
@ -17,4 +17,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec-rails', ['~> 2.0']
s.add_development_dependency 'capybara', ['~> 1.0']
s.add_development_dependency 'jasmine-rails', ['~> 0.4.5']
s.add_development_dependency 'turbolinks', ['~> 2.2.2']
s.add_development_dependency 'execjs', ['~> 2.1.0']
end

View file

@ -12,10 +12,15 @@
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require paloma
//= require_tree .
// $(document).on('page:restore', function(){
// Paloma.executeHook();
// });
//
//

View file

@ -1,12 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>TestApp</title>
<title>Paloma Test App</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<h1>Paloma Test App</h1>
<div class='links'>
<ul>
<li><%= link_to 'Main#index', main_index_path %></li>
<li><%= link_to 'Main#new', new_main_path %></li>
<li><%= link_to 'Main#show', main_path(1) %></li>
<li><%= link_to 'Main#edit', edit_main_path(1) %></li>
<li><%= link_to 'Main#prevent', prevent_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#file_response', file_response_main_index_path %></li>
</ul>
</div>
<hr/>
<%= yield %>

View file

@ -33,5 +33,5 @@ end
def request
page.evaluate_script 'Paloma.engine.lastRequest'
page.evaluate_script 'Paloma.engine.getRequest()'
end

View file

@ -2,31 +2,41 @@
var Engine = function(config){
this.factory = config.factory;
this.lastRequest = null;
this._request = null;
};
Engine.prototype.request = function(resource, action, params){
this.lastRequest = null;
var Controller = this.factory.get(resource);
Engine.prototype.start = function(){
var resource = this._request['controller'],
Controller = this.factory.get(resource);
if (!Controller){
return Paloma.warn('Paloma: undefined controller -> ' + resource);
}
var controller = new Controller(params);
var controller = new Controller( this._request['params'] ),
action = this._request['action'],
params = this._request['params'];
if (!controller[action]){
return Paloma.warn('Paloma: undefined action <' + action +
'> for <' + resource + '> controller');
}
Paloma.log('Paloma: Execute ' + resource + '#' + action + ' with');
Paloma.log(params);
controller[action]();
this.lastRequest = {controller: resource, action: action, params: params};
controller[ this._request['action'] ]();
};
Engine.prototype.setRequest = function(resource, action, params){
this._request = {controller: resource, action: action, params: params};
};
Engine.prototype.getRequest = function(key){
return (!key ? this._request : this._request[key]);
};

View file

@ -16,4 +16,15 @@
Paloma.engine = new Paloma.Engine({factory: Paloma._controllerFactory});
Paloma.executeHook = function(){
var $hook = $('.js-paloma-hook:first script:first');
if ($hook.length == 0){ return; }
var hook = $hook.html();
eval(hook);
};
})(window.Paloma);