From 9fc18534615934332c28e683d1ecb23c6f8f43ce Mon Sep 17 00:00:00 2001 From: kbparagua Date: Wed, 2 Mar 2016 11:27:36 +0800 Subject: [PATCH] Pass test --- .../app/assets/javascripts/application.js | 10 +--- test_app/spec/integration/basic_spec.rb | 4 +- test_app/spec/spec_helper.rb | 8 ++- vendor/assets/javascripts/paloma/engine.js | 57 +++++++++++-------- vendor/assets/javascripts/paloma/paloma.js | 12 +++- 5 files changed, 56 insertions(+), 35 deletions(-) diff --git a/test_app/app/assets/javascripts/application.js b/test_app/app/assets/javascripts/application.js index cd20709..1aa01aa 100644 --- a/test_app/app/assets/javascripts/application.js +++ b/test_app/app/assets/javascripts/application.js @@ -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(); }); }); }); diff --git a/test_app/spec/integration/basic_spec.rb b/test_app/spec/integration/basic_spec.rb index ccfe14e..6278406 100644 --- a/test_app/spec/integration/basic_spec.rb +++ b/test_app/spec/integration/basic_spec.rb @@ -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 diff --git a/test_app/spec/spec_helper.rb b/test_app/spec/spec_helper.rb index a24fe16..1c9d8c0 100644 --- a/test_app/spec/spec_helper.rb +++ b/test_app/spec/spec_helper.rb @@ -33,5 +33,9 @@ end def request - page.evaluate_script 'Paloma.engine.getRequest()' -end \ No newline at end of file + page.evaluate_script 'Paloma.engine.lastRequest()' +end + +def paloma_executed? + page.evaluate_script 'Paloma.isExecuted()' +end diff --git a/vendor/assets/javascripts/paloma/engine.js b/vendor/assets/javascripts/paloma/engine.js index b21aff5..d6f1acb 100644 --- a/vendor/assets/javascripts/paloma/engine.js +++ b/vendor/assets/javascripts/paloma/engine.js @@ -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; diff --git a/vendor/assets/javascripts/paloma/paloma.js b/vendor/assets/javascripts/paloma/paloma.js index c329c61..d65722d 100644 --- a/vendor/assets/javascripts/paloma/paloma.js +++ b/vendor/assets/javascripts/paloma/paloma.js @@ -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);