mirror of
https://github.com/kbparagua/paloma
synced 2023-03-27 23:21:17 -04:00
Working with turbolinks
This commit is contained in:
parent
a030303a4a
commit
05e413ce28
7 changed files with 48 additions and 22 deletions
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
//
|
||||
//= require jquery
|
||||
//= require jquery_ujs
|
||||
//= require turbolinks
|
||||
//= require paloma
|
||||
//= require_tree .
|
||||
|
||||
|
|
|
@ -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 %>
|
||||
|
||||
|
|
|
@ -33,5 +33,5 @@ end
|
|||
|
||||
|
||||
def request
|
||||
page.evaluate_script 'Paloma.engine.lastRequest'
|
||||
page.evaluate_script 'Paloma.engine.getRequest()'
|
||||
end
|
28
vendor/assets/javascripts/paloma/engine.js
vendored
28
vendor/assets/javascripts/paloma/engine.js
vendored
|
@ -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]);
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue