Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pageable Collection #5

Open
dottodot opened this issue Feb 10, 2015 · 6 comments
Open

Pageable Collection #5

dottodot opened this issue Feb 10, 2015 · 6 comments

Comments

@dottodot
Copy link

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants