Pages

Tuesday, July 10, 2012

Adding HTTP headers to AJAX calls in Backbone.js

Just a quick note about setting custom header in the HTTP request that Backbone.js manages. This is super easy to do. In the example code below, I have a Backbone.Collection assigned to messagesCollection variable and I'm triggering a fetch on that collection: messagesCollection.fetch({ headers: { 'x-my-custom-header-1':'foobar', 'x-my-rest-api-version':'1.0' } });

14 comments:

  1. Thanks for this, I couldn't find anything about headers in the Backbone docs.

    Also, if anyone wants to add default headers (such as a REST token) to all their backbone sync calls, you can do this:

    (function(){
    // Store the native, default sync method, we'll wrap this.
    Backbone._nativeSync = Backbone.sync;

    // Create our default options object
    Backbone.defaultSyncOptions = {};

    // Ever so gingerly wrap the native sync
    // in a method that combines options
    Backbone.sync = function( method, model, options) {
    Backbone._nativeSync( method, model,
    _.extend( {}, Backbone.defaultSyncOptions, options ) );
    };
    })();

    // then just set the options:
    Backbone.defaultSyncOptions = { headers: { "foo":"bar" } };

    ReplyDelete
  2. Tyler, thanks for posting that.

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

    ReplyDelete
  4. You can find anyone quite easily these days once you have a computer that is connected to the internet. That is one of the good sides of the internet technology; all unlisted telephone numbers can now be traced easily. I must confess that a lot of mobile phone and information technology experts went to work after the WTC bombings a few years ago; and of course, this brought about various means of tracing cell free business calls

    ReplyDelete
  5. Do you want to find out more about android apps, which ones are the best, where to download them and how they can improve your experience with the android platform? If so, keep reading this article to find out the most important things you have to know about android apps. Source

    ReplyDelete
  6. If it behaves like a drug, and possesses the same characteristics of a drug, then it may very well be a... As many of you know, I have discussed the adverse effects of technology on numerous occasions. Specifically, I am talking about such things as mobile phones, video games, tablets and personal computers, those devices we embrace in the daily affairs of our lives. mobile tracker

    ReplyDelete
  7. There are some interesting points in time in this article but I do not determine if I see every one of them center to heart. You can find some validity but I will take hold opinion until I investigate it further. Excellent write-up , thanks and now we want far more! Added to FeedBurner in addition Mega888 android and ios

    ReplyDelete
  8. If it behaves like a drug, and possesses the same characteristics of a drug, then it may very well be a... As many of you know, I have discussed the adverse effects of technology on numerous occasions. Specifically, I am talking about such things as mobile phones, video games, tablets and personal computers, those devices we embrace in the daily affairs of our lives. camera spy

    ReplyDelete
  9. Thank you for your very good information and feedback from you. san jose used car 토토사이트

    ReplyDelete
  10. Closed Circuit Television (CCTV) comprised of mini security cameras can catch criminals in the act and give police valuable information for the quickest response rates to crimes. Cost of security cameras have dropped significantly in recent years making it a viable option for homeowners. how to make a wifi spy camera

    ReplyDelete
  11. I believe you have mentioned some very interesting points , regards for the post. digital banking software

    ReplyDelete