Backbone.js: urlRoot with http query string?

11,141

Solution 1

You will have to use a custom url function for the model.

Book.url = function() {
  return this.urlRoot + '/' + this.id + '?details=true';
};

Solution 2

You can also use the option for the method fetch

mybook.fetch({data:{details: true}});
Share:
11,141
Lai Yu-Hsuan
Author by

Lai Yu-Hsuan

Updated on June 06, 2022

Comments

  • Lai Yu-Hsuan
    Lai Yu-Hsuan almost 2 years

    In Backbone.js I can appoint where the model fetches it's data:

    var Book = Backbone.Model.extend({urlRoot : '/books'});
    var mybook = new Book({id: "1"});
    mybook.fetch();  //it will access '/books/1'
    

    But if I want to append a query string after the URL? e.g. the book data is at /books/1&details=true. Can I specify this in model?

  • Tom
    Tom over 10 years
    Although the accepted comment works, this seems to be the 'backbone way'
  • talltolerablehuman
    talltolerablehuman over 8 years
    I wish there was a way to define it on a model/collection level. this way you have to pass the data each time you call the fetch, would be nice if you could just set the data once on the model itself.