Lai Yu-Hsuan
Lai Yu-Hsuan

Reputation: 28111

Backbone.js: urlRoot with http query string?

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?

Upvotes: 21

Views: 11177

Answers (2)

Shuping
Shuping

Reputation: 5468

You can also use the option for the method fetch

mybook.fetch({data:{details: true}});

Upvotes: 45

abraham
abraham

Reputation: 47833

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

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

Upvotes: 22

Related Questions