Search code examples
javascriptajaxbackbone.jspaginationbackgrid

Backgrid paginator withCredentials


I'm using this component https://github.com/wyuenho/backgrid-paginator to handle a pageable collection.

I'm using authentication (passport) for all my server calls and for this reason i put xhrFields: { withCredentials: true} on my ajax methods.

The problem comes when i use the link of the paginator component

enter image description here

It does not use withCredentials options and my request is denied.

How can i add the above option to that calls?


Solution

  • Backgrid Paginator has nothing to do with your request. It just provides you a view to handle pagination. What you have to do is to adapt your Collection request. The collection needs to be a backbone.paginator collection ( Backbone.PageableCollection ) which extends Backbone.Collection.

    Long story short, the collection that you are going to instantiate needs to have this behavior added by extending the fetch: function(){} method and pass the options needed. But since you mentioned that for all your sever calls you pass that option, you should use jQuery.ajaxSetup() - even if it's use is not recommended, in your case it applies.

    Make sure you call $.ajaxSetup() once, preferably before your app loads but jQuery is loaded.

     $.ajaxSetup({
         xhrFields: {
            withCredentials: true
         }
      });