mardi 24 septembre 2019

Stop rails controller method execution if xhr request is cancelled? Rails API

I'm trying to create an SPA with react frontend and a rails API, and I managed to stop the request from the javascript side, with that in mind, I notice that even if I cancel the request, the rails server stills performs the stuff inside the controller.

    def index
      @dashboards = Dashboard.all
      # Rest of controller logic, like calls to an external API

      render json: json_response(@dashboards)
    end

Is there a way to catch that the xhr request was cancelled so I can do something like this:

    def index
      @dashboards = Dashboard.all
      if request.cancelled? throw :abort 
      # It should not matter since request was cancelled in the frontend

      render json: json_response(@dashboards)
    end

Aucun commentaire:

Enregistrer un commentaire