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

Pass test

This commit is contained in:
kbparagua 2016-03-02 11:27:36 +08:00
parent 7b8cc9cac5
commit 9fc1853461
5 changed files with 56 additions and 35 deletions

View file

@ -19,10 +19,7 @@
// Uncomment if jquery.turbolinks is not used. // Uncomment if jquery.turbolinks is not used.
// $(document).on('page:load', function(){ // $(document).on('page:load', function(){ Paloma.start(); });
// Paloma.executeHook();
// Paloma.engine.start();
// });
// //
@ -57,15 +54,14 @@ NotFoos.prototype.otherAction = function(){};
$(document).ready(function(){ $(document).ready(function(){
Paloma.engine.start(); Paloma.start();
$('#js-ajax-link').on('click', function(e){ $('#js-ajax-link').on('click', function(e){
e.preventDefault(); e.preventDefault();
$.get($(this).prop('href'), function(response){ $.get($(this).prop('href'), function(response){
$('#js-ajax-response').html(response); $('#js-ajax-response').html(response);
Paloma.executeHook(); Paloma.start();
Paloma.engine.start();
}); });
}); });
}); });

View file

@ -125,7 +125,7 @@ feature 'executing Paloma controller', :js => true do
context 'controller#action at first then false' do context 'controller#action at first then false' do
it 'does not execute any js' do it 'does not execute any js' do
visit multiple_calls_4_main_index_path visit multiple_calls_4_main_index_path
expect(request).to be_nil expect(paloma_executed?).to be_falsy
end end
end end
@ -163,7 +163,7 @@ feature 'executing Paloma controller', :js => true do
include_examples 'no paloma' include_examples 'no paloma'
it 'prevents execution of Paloma controller' do it 'prevents execution of Paloma controller' do
expect(request).to be_nil expect(paloma_executed?).to be_falsy
end end
end end

View file

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

View file

@ -2,44 +2,39 @@
var Engine = function(config){ var Engine = function(config){
this.factory = config.factory; this.factory = config.factory;
this._request = null; this._clearRequest();
}; };
Engine.prototype.start = function(){ Engine.prototype.start = function(){
// Do not execute if there is no request available.
if ( !this._request ) return; if ( !this._request ) return;
// Do not execute if already executed. this._lastRequest = this._request;
//
// 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;
var resource = this._request['controller'], var resource = this._request.controller,
Controller = this.factory.get(resource); Controller = this.factory.get(resource);
if (!Controller){ if ( !Controller )
return Paloma.warn('Paloma: undefined controller -> ' + resource); return this._stopWithWarning(
} 'Paloma: undefined controller -> ' + resource
);
var controller = new Controller( this._request['params'] ), var controller = new Controller( this._request.params ),
action = this._request['action'], action = this._request.action,
params = this._request['params']; params = this._request.params;
if (!controller[action]){
return Paloma.warn('Paloma: undefined action <' + action +
'> for <' + resource + '> controller');
}
if ( !controller[action] )
return this._stopWithWarning(
'Paloma: undefined action <' + action + '> for <' +
resource + '> controller'
);
Paloma.log('Paloma: Execute ' + resource + '#' + action + ' with'); Paloma.log('Paloma: Execute ' + resource + '#' + action + ' with');
Paloma.log(params); 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]); 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; Paloma.Engine = Engine;

View file

@ -16,7 +16,7 @@
Paloma.engine = new Paloma.Engine({factory: Paloma._controllerFactory}); Paloma.engine = new Paloma.Engine({factory: Paloma._controllerFactory});
Paloma.executeHook = function(){ Paloma._executeHook = function(){
var hook = document.getElementsByClassName('js-paloma-hook')[0]; var hook = document.getElementsByClassName('js-paloma-hook')[0];
if (!hook) return; if (!hook) return;
@ -26,5 +26,15 @@
eval(script.innerHTML); 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); })(window.Paloma);