Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7916073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:40:25+00:00 2026-06-03T14:40:25+00:00

I have some trouble appending a token to the backbone url query string and

  • 0

I have some trouble appending a token to the backbone url query string and hope you guys could help me out here. Three things to know,

  • There is a rest api that expects a token with each request
  • An nginx backend that does auth, serves the backbone app + proxy req to the api under /api
  • i’m a new to javascript + backbone :/

The backbone app actually reads the token from a cookie and I need to append this to the request url everytime backbone makes a call. I see this can be done by overriding backbone sync. but it troubles me in a few different things. like, this is what I do

console.log('overriding backbone sync');
var key ="token";
Backbone.old_sync = Backbone.sync
Backbone.sync = function(method, model, options) {
    if (method === 'read') {
        if (!(model.url.indexOf('?key=') != -1)) {
            model.url = model.url + '?key=' + key;
        }
    } else {
        old_url = model.url();
        if (!(old_url.indexOf('?key=') != -1)) {
            model.url = function() {
                return old_url + '?key=' + key;
            }
        }
    }
    Backbone.old_sync(method, model, options);
};

model.url was returning a function when its not a “read” method and didn’t know how to handle it well and the other trouble is when a consecutive request is made, the token is added twice. I tried to remove it with that indexOf stuff with no luck.

Is there a better way to do this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:40:28+00:00Added an answer on June 3, 2026 at 2:40 pm

    I don’t think you need to override sync at all:

    var globalKey = 'key123';
    
    var urlWithKey = function(url, key) {
        return function() {
            return url + "?key=" + key;
        };
    };
    
    var MyModel = Backbone.Model.extend({
        url: urlWithKey('/my/url/', globalKey)
    });
    

    If you now create an object and save it, a POST request to my/url/?key=key123 is sent.
    I guess you could also override the url method if this is the behavior you need for all of your Backbone models.

    A general note: in Backbone most parameters, such as url can be a function or a value. I don’t know why in your example it was a function once and a value in another case, but you always must be able to handle both ways if you override some of the internal functions. If you look at Backbone’s sourcecode you will see that they use getValue to access these parameters:

    var getValue = function(object, prop) {
        if (!(object && object[prop])) return null;
        return _.isFunction(object[prop]) ? object[prop]() : object[prop];
    };
    

    Update: Overloading the url method for all models could work like this:

    var globalKey = 'key123';
    
    (function() {
        var baseUrl = Backbone.Model.prototype.url;
        Backbone.Model.prototype.url = function() {
            return this.baseUrl + "?key=" + globalKey;
        };
    })()
    
    var MyModel = Backbone.Model.extend({
       baseUrl: '/my/url/'
    });
    

    You could also leave the regular Backbone.Model as it is, and create your own base class. See http://documentcloud.github.com/backbone/#Model-extend for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm have some trouble with the fulltext CONTAINS operator. Here's a quick script to
I have some trouble using the Zend url helper with get parameter. In a
I am having some trouble with a LINQ to dataset query. I have the
i have some trouble with the custom shapes in corona. Here is my code
I have some trouble of figuring out how to use group_concat to produce certain
I have some trouble using an Ajax query in a Rails + JQuery ecosystem.
I have some trouble figuring out the best way to store my programming todo
I have some trouble showing a disabled Form in non-modal state. Here is the
I have some trouble figuring out why the following crashes (MSVC9): //// the following
I have some trouble with my trac installation (version 11.4). What should I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.