Break filter chain when redirect occurs #526

Merged
merged 4 commits into from Aug 20, 2012

5 participants

@kristianpd

@hornairs @nciagra I'm looking to break filter chains if a redirect call is made within a beforeFilter to avoid continuing with remaining filters and avoid action from executing on the controller. This current solution is simply inspecting _afterFilterRedirect to see if a redirect has been requested.

I've noticed an issue which I believe may be related to pushState on the Navigator (possibly unrelated to this change) where the following scenario occurs:

class FoosController extends Batman.Controller
  @beforeFilter: (options) -> 
     @redirect @get('routes.foos.other')  if options.action is 'index'

  index: -> 
  show: ->
  other: -> 

If this is the first request to the application (i.e. a new browser session), a request for
/foos
will result in an address bar URL of
/foos/other as you'd expect.

If a successful dispatch has already occurred, say to
/foos/1
or any other controller action, a request to
/foos
will redirect and render accordingly, but the URL in the address bar will remain as
/foos
ignoring the redirected URL of
/foos/other
though a brief flicker indicates it may have been pushed to the Navigator in some manner

Thoughts about this approach so far or insights into the pushState issue?

@travisbot

This pull request passes (merged 86a309a into da0d145).

@airhorns

Is it really only redirects that should short circuit the rest of the dispatch? Ithink thats right. I have no insight into the pushState issue, sorry. I will look over the weekend.

@airhorns airhorns and 1 other commented on an outdated diff Aug 17, 2012
src/controller/controller.coffee
@@ -77,7 +77,7 @@ class Batman.Controller extends Batman.Object
parentFrame = @_actionFrames[@_actionFrames.length - 1]
frame = new Batman.ControllerActionFrame {parentFrame, action}, =>
- @_runFilters action, params, 'afterFilters'
+ @_runFilters action, params, 'afterFilters' unless @_afterFilterRedirects
@airhorns
airhorns added a line comment Aug 17, 2012

Redirects? not Redirect?

@kristianpd
kristianpd added a line comment Aug 17, 2012

Yup, i have this in a follow up commit inbound

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@airhorns airhorns and 1 other commented on an outdated diff Aug 17, 2012
src/controller/controller.coffee
@@ -87,8 +87,7 @@ class Batman.Controller extends Batman.Object
oldRedirect = Batman.navigator?.redirect
Batman.navigator?.redirect = @redirect
@_runFilters action, params, 'beforeFilters'
-
- result = @[action](params)
+ result = @[action](params) unless @_afterFilterRedirect
@airhorns
airhorns added a line comment Aug 17, 2012

Why not just return?

@kristianpd
kristianpd added a line comment Aug 20, 2012

I didn't want to mess with the ControllerFrameAction or return result of the executeAction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@smathy

Very interesting. We had our own redirect issues this week. I'm still working out how to write a good test for it, but we had a problem where if you had one redirect to a view, and another redirect after that view was displayed, then we ended up in an infinite loop because Batman.navigator.redirect pointed to the OurController.redirect

We discovered that oldRedirect - just above the code in this PR was "unravelling" in the wrong order, leaving the wrong value as the last value assigned to Batman.navigator.redirect

My code is here: https://github.com/amco/batman/compare/redirect_loop

Your tests just test your exact change, rather than the underlying URL problem. I wonder if your underlying problem is caused by the same thing my problem was?

@airhorns

LGTM 👍 :shipit:

@travisbot

This pull request passes (merged 9ffa1e4 into 5d1ff82).

@kristianpd kristianpd merged commit 4bad808 into master Aug 20, 2012

1 check passed

Details default The Travis build passed
@rmosolgo rmosolgo removed the Code Review label Aug 7, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment