Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Encountered the same problem a few months back doing a single-page app that consumes a REST based api. What I came up with after searching for answers was to use HTTP's existing 401 and 403 error. I had my api return these errors. Then caught the exceptions by using an extended error handling model to handle these errors and just routed them to my login via the router <a href="http://backbonejs.org/#Router-navigate" rel="nofollow noreferrer">navigate</a> function. </p> <pre><code>var ErrorHandlerModel = Backbone.Model.extend({ initialize: function(attributes, options) { options || (options = {}); this.on("error", this.errorHandler); this.init &amp;&amp; this.init(attributes, options); }, errorHandler: function(model, error) { if (error.status == 401 || error.status == 403) { app.history.navigate('login', true); } } }); </code></pre> <p>In hindsight though i think it would have been better to just use a global jquery <a href="http://api.jquery.com/ajaxError/" rel="nofollow noreferrer">ajaxError</a> function instead. Snippet above was based on a similar <a href="https://stackoverflow.com/questions/6150514/global-error-handler-for-backbone-js-ajax-requests">question posted here</a> a few months back. </p> <p>I also had to override backbones default fetch behavior so i could trigger an error with the ogin to catch a response variable included in the json response of the api.</p> <pre><code>var Login = Backbone.Model.extend({ urlRoot: '/login', parse: function(resp,xhr) { if (resp.response == 'success') { app.history.navigate('dashboard', true); } else { this.trigger('loginError'); } return false; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload