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' } });

26 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
  12. As being a Newbie, We’re permanently exploring online for articles which can be of help to me. Many thanks buy steroids online

    ReplyDelete
  13. Can I just say what a aid to seek out someone who really knows what theyre speaking about on the internet. You definitely know the way to carry a difficulty to mild and make it important. More people need to read this and perceive this aspect of the story. I cant imagine youre not more fashionable because you positively have the gift. buy hgh

    ReplyDelete
  14. I simply had to thank you very much once again. I’m not certain what I could possibly have followed in the absence of the type of ideas contributed by you concerning my theme. It actually was a frightful difficulty for me, nevertheless finding out the very professional manner you resolved the issue forced me to jump over joy. Now i’m thankful for your work and even hope that you are aware of a great job you’re doing training people today thru your blog post. I know that you have never got to know any of us. hgh for sale

    ReplyDelete
  15. I would like to thank you for the efforts you have put in writing this Magic Mesh Door Coverblog. I’m hoping the same high-grade blog post from you in the future also. Actually your creative writing abilities has inspired me to get my own screen door instant website going now. Actually blogging is spreading its wings and growing fast. Your write up is a great example. clenbuterol for sale

    ReplyDelete
  16. I would like to thank you for the efforts you have put in writing this Magic Mesh Door Coverblog. I’m hoping the same high-grade blog post from you in the future also. Actually your creative writing abilities has inspired me to get my own screen door instant website going now. Actually blogging is spreading its wings and growing fast. Your write up is a great example. Yoga

    ReplyDelete
  17. You were quite interesting.. But sadly I didnrrrt trust them much :/ Although I might disagree I still give you support as how confident you are on your writing lol mens steampunk leather jacket

    ReplyDelete
  18. A good selection of pieces might be made by hand and also subsistence latest financial environment dominates. Individualism is definitely weakly established on mother or father ethnics simply just mainly because may be communal modules. Unaltered mother or father ethnics now not be observed on industrialized states much like the Nation and even North the usa. womens silk pajamas

    ReplyDelete
  19. Youre so cool! I dont suppose Ive read anything similar to this just before. So nice to uncover somebody by original applying for grants this subject. realy thank you for starting this up. this amazing site is one area that is required on the internet, someone after some originality. useful work for bringing a new challenge for the web! zavrol zavrol

    ReplyDelete
  20. There is no dearth of options and possibilities that can be explored with mobile apps for making your business high on the effectiveness metrics. Moreover mobile app development provides scope for your imagination to rein free and create what you need. singapore best mobile app services

    ReplyDelete
  21. wow..amazing post blog that understand the value of providing a quality resource for free
    click the below links
    Best Astrologer in Banashankari

    ReplyDelete
  22. Nice Post! I liked it.

    abhiram astrology center. Best Astrologer In nelson

    ReplyDelete