0

I am new to Backbone, NodeJS and web development and am struggling with the proper design of my Backbone Collection and Model GET requests and routes on my NodeJS server.

At first I only used Models and specified the urlRoot which worked perfectly. Once I added my Models to a Collection it used the Collections' url parameter which was fine once I realized what was happening.

Say I have model "Book" and a collection "Library" defined as below:

Book

app.Book = Backbone.Model.extend({
   defaults: {
       title: 'No title',
       author: 'Unknown'
   }
});

Library

app.Library = Backbone.Collection.extend({
    model: app.Book,
    url: '/books'
});

Now if I do a GET request on the model the url will be '/books/:id which has a route specified on my node js server and I would return the book with the specified id.

My Question

I don't know what is good practice and design regarding the GET request on the Collection. Say if I want to fetch a Collection of books by a certain author my url will look something like /books/:author_id, but this would go to the same route as my Model GET request on my node js server.

How do I distinguish between the Model GET request and Collection GET request on my node server? I don't want to use a quick hack, but would like design it properly.

What is considered best practice to pass 'filter' parameters with a Collection GET request?

Jordan Running
  • 91,621
  • 15
  • 164
  • 165
Timo
  • 199
  • 1
  • 7
  • possible duplicate of [REST API Best practices: Where to put parameters?](http://stackoverflow.com/questions/4024271/rest-api-best-practices-where-to-put-parameters) – Jordan Running Mar 05 '15 at 20:38

1 Answers1

0

When we need to have multiple filters like that we do something like this.

/books/:id

/books/byAuthor/:authorId

/books/bySearchTerm/:searchTerm

Although in our case we're using Angular and Repos to get our data, but the url pattern works regardless of what you're client side is structured like.

Zipper
  • 6,684
  • 6
  • 44
  • 64
  • So the Model will use the default `url` on the Collection, but you would then change the `url` before you do a `fetch` on the Collection? – Timo Mar 05 '15 at 20:47
  • That's what we did when we used Backbone. Although to be honest we found backbone difficult to use as complexity grew, and we went with a Angular system, and used normal javascript objects in a standard DDD style. – Zipper Mar 06 '15 at 02:53