Coder Social home page Coder Social logo

Comments (3)

eugenet8k avatar eugenet8k commented on June 26, 2024

We are having the same challenge at the moment. Have you ever found an elegant solution for this problem? The ideal way is to return bool value in beforeAction() that would instruct the controller to halt normal flow.

from chaplin.

molily avatar molily commented on June 26, 2024

In order to cancel the normal flow, we need to know that a redirect happened in the before action. The preferred way of initiating a redirect is the Controller#redirectTo method (see http://docs.chaplinjs.org/chaplin.controller.html). It sets a redirected = true flag on the controller:

https://github.com/chaplinjs/chaplin/blob/master/src/chaplin/controllers/controller.coffee#L52-L55

We’re checking for the redirected flag in the Dispatcher:

https://github.com/chaplinjs/chaplin/blob/master/src/chaplin/dispatcher.coffee#L133

In your cases you’re not using Controller#redirectTo, but the redirect is triggered somewhere else. this is totally fine, but the redirected flag is not set. As far as I can see, you can set this flag manually in your before action. This should stop the normal action from being called.

beforeAction: ->
  unless loggedIn()
    @redirected = true
    mediator.publish('event that eventually triggers a redirect')

from chaplin.

eugenet8k avatar eugenet8k commented on June 26, 2024

Thanks @molily, this helps. It seems my problem was in using Chaplin utility utils.redirectTo() method in beforeAction() instead of Controller.redirectTo() that sets redirected flag.

from chaplin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.