0
votes

My new application main collection is still very basic:

var TransactionCollection = Backbone.Collection.extend({
    model: Transaction,
    url: '/api/transactions'
});

This is good while developing, but now I'd like to add some pagination and filtering features.

I'm not sure about what URL schema I should use. I mean:

  • page 1, all transactions, January 2014 => /api/transactions/2014/01
  • page 2, all transactions, January 2014 => /api/transactions/2014/01?p=2
  • page 1, all transactions, February 2014 => /api/transactions/2014/02
  • page 1, only incomes, June 2014 => /api/transactions/incomes/2014/06
  • page 2, only outcomes, June 2014 => /api/transactions/outcomes/2014/06/?p=2

Is it a good structure, in your opinion?

In each case, what's the best practice for managing such a collection? I guess I can make the url: property a function, to dynamically handle several parameters (page, filter, period) passed to the collection before fetching it. Or is it better to pass parameters with each fetch?

1
ok so I should use GET query parameters for all above cases, right?Fabio B.
yes, I added examples below in my answer for each your urlEugene Glova

1 Answers

2
votes

Based on best practices

  • page 1, all transactions, January 2014 => /api/transactions?year=2014&month=01
  • page 2, all transactions, January 2014 => /api/transactions?year=2014&month=01&offset=20&limit=20
  • page 1, all transactions, February 2014 => /api/transactions?year=2014&month=02
  • page 1, only incomes, June 2014 => /api/transactions?year=2014&month=06&filter=incomes
  • page 2, only outcomes, June 2014 => /api/transactions?year=2014&month=06&filter=outcomes&offset=20&limit=20

To send the query string use data attribute in ajax options like this

transactionCollection.fetch({
    data: {
        limit: 20,
        offset: 20,
        year: "2014",
        month: "01"
    }
});