ianhaggerty / backbone-sails

A plugin for Backbone that makes integrating with Sails JS easier. Both leverage a model-view architecture, it's only natural to have them talk to each other.
MIT License
7 stars 1 forks source link

Pageable Collection #5

Open dottodot opened 9 years ago

dottodot commented 9 years ago

I'm looking to change one of my existing apps to using Backbone.Sails but a lot of the collections use backbone.paginator and I'm assuming the 2 plugins wont work with each other.

Some of the functionality can be achieved by skip and limit in the query but I can't think of a sensible way of getting the total records so number of pages can be calculated as I don't believe there is a way to do a count without fetching the entire collection.

Any suggestions would be greatly appreciated.

ianhaggerty commented 9 years ago

I don't know much about the internals of backbone.paginator. It does extend from the collection class to do it's work, which makes compatibility with Backbone.Sails an issue...

It wouldn't take long to produce paginated class which extends from Backbone.Sails.Collection. I'll take a look at it tonight & pull what I come up with into the v0.2 branch.

ianhaggerty commented 9 years ago

Just taking a look at the functionality of backbone.paginator. Some good stuff! I have been digging into the source. Both plugins overload the fetch() method unfortunately, making compatibility impossible without editing the internals of one or the other.

I am going to try to reproduce similar functionality for the v0.2 release. Hopefully it'll give this plugin a greater base of appeal.

In the meantime, if your pagination needs are relatively simple, you can always write some quick functions on your class:

coll = Backbone.Sails.Collection.extend({
  resultsPerPage: 10,
  getPage: function(num) {
    // first page is zero
    this.query({ limit: this.resultsPerPage, skip: num * this.resultsPerPage });
    return this.fetch();
  }
})
ianhaggerty commented 9 years ago

So far as counting is concerned, Sails can count the number of objects in a collection User.count(function(err, num){ }), this isn't subject to where criteria however. I'll change the blueprints to respond to a flag to return this information. It'll have to be over HTTP headers though.

dottodot commented 9 years ago

Ok thanks for that. The count is only required so the number of pages can be calculated, for just a next and prev page setup this isn't really necessary.

dottodot commented 9 years ago

For the total records could it be as simple as adding the following to the find blueprint?

queryCount = Model.count().where(actionUtil.parseCriteria(req)).exec(function(err, count) {
      res.set('Total-Records', count);
    });
ianhaggerty commented 9 years ago

Pretty much :)

Feel free to add yourself. The blueprints are intended to be customizable after all.

NB: I won't be working on Backbone.Sails until the summer. I simply don't have the time at the moment.