Search code examples
backbone.js

How to set a Collection's url


let's say I have :

var Book = Backbone.Model.extend();

var Collection = Backbone.Collection.extend({
  model: Book,
  url: '/books',
  initialize: function(){
    this.fetch();
  })
})

How can I change the Collection's url when instantiating a new collection ?

var AdventureBooks = new Books({ url: '/books/adventure' }) does not work

var AdventureBooks = new Books({ category: 'adventure' })

and in the Collection definition:

url : '/books/' + this.category does not work either.

Thanks.


Solution

  • var Book = Backbone.Model.extend({
      "url": function() {
        return '/books/' + this.get("category");
      }
    });