mirror of
https://github.com/kbparagua/paloma
synced 2023-03-27 23:21:17 -04:00
Pass test
This commit is contained in:
parent
7b8cc9cac5
commit
9fc1853461
5 changed files with 56 additions and 35 deletions
|
@ -19,10 +19,7 @@
|
|||
|
||||
|
||||
// Uncomment if jquery.turbolinks is not used.
|
||||
// $(document).on('page:load', function(){
|
||||
// Paloma.executeHook();
|
||||
// Paloma.engine.start();
|
||||
// });
|
||||
// $(document).on('page:load', function(){ Paloma.start(); });
|
||||
|
||||
|
||||
//
|
||||
|
@ -57,15 +54,14 @@ NotFoos.prototype.otherAction = function(){};
|
|||
|
||||
$(document).ready(function(){
|
||||
|
||||
Paloma.engine.start();
|
||||
Paloma.start();
|
||||
|
||||
$('#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();
|
||||
Paloma.start();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -125,7 +125,7 @@ feature 'executing Paloma controller', :js => true do
|
|||
context 'controller#action at first then false' do
|
||||
it 'does not execute any js' do
|
||||
visit multiple_calls_4_main_index_path
|
||||
expect(request).to be_nil
|
||||
expect(paloma_executed?).to be_falsy
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -163,7 +163,7 @@ feature 'executing Paloma controller', :js => true do
|
|||
include_examples 'no paloma'
|
||||
|
||||
it 'prevents execution of Paloma controller' do
|
||||
expect(request).to be_nil
|
||||
expect(paloma_executed?).to be_falsy
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -33,5 +33,9 @@ end
|
|||
|
||||
|
||||
def request
|
||||
page.evaluate_script 'Paloma.engine.getRequest()'
|
||||
end
|
||||
page.evaluate_script 'Paloma.engine.lastRequest()'
|
||||
end
|
||||
|
||||
def paloma_executed?
|
||||
page.evaluate_script 'Paloma.isExecuted()'
|
||||
end
|
||||
|
|
57
vendor/assets/javascripts/paloma/engine.js
vendored
57
vendor/assets/javascripts/paloma/engine.js
vendored
|
@ -2,44 +2,39 @@
|
|||
|
||||
var Engine = function(config){
|
||||
this.factory = config.factory;
|
||||
this._request = null;
|
||||
this._clearRequest();
|
||||
};
|
||||
|
||||
Engine.prototype.start = function(){
|
||||
// Do not execute if there is no request available.
|
||||
if ( !this._request ) return;
|
||||
|
||||
// Do not execute if already executed.
|
||||
//
|
||||
// This happens when using turbolinks,
|
||||
// if a page using js(false) is rendered
|
||||
// after a page with executed js, then the
|
||||
// previous js will be executed again.
|
||||
if ( this._request.executed ) return;
|
||||
this._lastRequest = this._request;
|
||||
|
||||
var resource = this._request['controller'],
|
||||
var resource = this._request.controller,
|
||||
Controller = this.factory.get(resource);
|
||||
|
||||
if (!Controller){
|
||||
return Paloma.warn('Paloma: undefined controller -> ' + resource);
|
||||
}
|
||||
if ( !Controller )
|
||||
return this._stopWithWarning(
|
||||
'Paloma: undefined controller -> ' + resource
|
||||
);
|
||||
|
||||
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');
|
||||
}
|
||||
var controller = new Controller( this._request.params ),
|
||||
action = this._request.action,
|
||||
params = this._request.params;
|
||||
|
||||
if ( !controller[action] )
|
||||
return this._stopWithWarning(
|
||||
'Paloma: undefined action <' + action + '> for <' +
|
||||
resource + '> controller'
|
||||
);
|
||||
|
||||
Paloma.log('Paloma: Execute ' + resource + '#' + action + ' with');
|
||||
Paloma.log(params);
|
||||
|
||||
controller[ this._request['action'] ]();
|
||||
controller[action]();
|
||||
|
||||
this._request.executed = true;
|
||||
this._lastRequest.executed = true;
|
||||
this._clearRequest();
|
||||
};
|
||||
|
||||
|
||||
|
@ -65,6 +60,22 @@
|
|||
return (!key ? this._request : this._request[key]);
|
||||
};
|
||||
|
||||
Engine.prototype.lastRequest = function(){
|
||||
return this._lastRequest || {executed: false};
|
||||
};
|
||||
|
||||
Engine.prototype.hasRequest = function(){
|
||||
return this._request != null;
|
||||
};
|
||||
|
||||
Engine.prototype._clearRequest = function(){
|
||||
this._request = null;
|
||||
};
|
||||
|
||||
Engine.prototype._stopWithWarning = function(warning){
|
||||
Paloma.warn(warning);
|
||||
this._clearRequest();
|
||||
};
|
||||
|
||||
Paloma.Engine = Engine;
|
||||
|
||||
|
|
12
vendor/assets/javascripts/paloma/paloma.js
vendored
12
vendor/assets/javascripts/paloma/paloma.js
vendored
|
@ -16,7 +16,7 @@
|
|||
|
||||
Paloma.engine = new Paloma.Engine({factory: Paloma._controllerFactory});
|
||||
|
||||
Paloma.executeHook = function(){
|
||||
Paloma._executeHook = function(){
|
||||
var hook = document.getElementsByClassName('js-paloma-hook')[0];
|
||||
if (!hook) return;
|
||||
|
||||
|
@ -26,5 +26,15 @@
|
|||
eval(script.innerHTML);
|
||||
};
|
||||
|
||||
Paloma.start = function(){
|
||||
if ( !this.engine.hasRequest() ) this._executeHook();
|
||||
if ( this.engine.hasRequest() ) this.engine.start();
|
||||
};
|
||||
|
||||
Paloma.isExecuted = function(){
|
||||
return this.engine.lastRequest().executed;
|
||||
};
|
||||
|
||||
|
||||
|
||||
})(window.Paloma);
|
||||
|
|
Loading…
Reference in a new issue