npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ampersand-paginated-subcollection

v3.0.0

Published

Filter an ampersand collection by limit and offset

Downloads

423

Readme

ampersand-paginated-subcollection

Lead Maintainer: Michael Garvin

Purpose

Paginated subset of a collection. Only emits reset events.

This allows you to have a paginated version of anything that acts like an ampersand-collection That you can quickly paginate through without affecting its parent.

Part of the Ampersand.js toolkit for building clientside applications.

install

npm install ampersand-paginated-subcollection

example

var WidgetCollection = require('./mycollection');
var PaginatedSubcollection = require('ampersand-paginated-subcollection');

var widgets = new WidgetCollection();

widgets.fetch();

// This will create a collection-like object
// that will only include 10 widgets starting
// at offset 0
var widgetPage = new PaginatedSubcollection(widgets, {
    limit: 10
});

//Move to the next 10 widgets
widgetPage.configure({offset: 10});

API reference

new PaginatedSubcollection(collection, [config]);

  • collection {Collection} An instance of ampersand-collection or Backbone.Collection that contains our full set of models.
  • config {Object} [optional] The config object which can take the following options
    • limit {Number} [optional] If specified will limit the number of models to this maximum number.
    • offset {Number} Default: 0. This is the index of the start of the current page of models to pull from collection

.configure(config, [reset])

This is how you paginate post-init

  • config {Object} Same config object as what you pass to init.
  • reset {Boolean} Default: false. If true, a reset event will be emitted regardless of whether or not the limit or offset were changed.

.at(index)

  • index {Number} returns model as specified index in the paginated collection.

.length

The paginated collection maintains a read-only length property that simply proxies to the array length of the models it contains.

.models

The array of filtered models with offset and/or limit applied (if set).

.isCollection

This property is present and set to true. see ampersand-collection for more info

PaginatedSubcollection.extend(mixins...)

PaginatedSubcollection attaches extend to the constructor so if you want to add custom methods to your PaginatedSubcollection constructor, it's easy:

var PaginatedSubcollection = require('ampersand-paginated-subcollection');

// this exports a new constructor that includes
// the methods you passed on the prototype while
// maintaining the inheritance chain for instanceof
// checks.
module.exports = PaginatedSubcollection.extend({
    myMethod: function () { ... },
    myOtherMethod: function () { ... }
});

This is done by using: ampersand-class-extend

A note about events

Because ampersand-view and its ilk cache views, it is really inexpensive to simply emit a reset whenever we want to change the pagination. This prevents us from having to do internal indexOf tests (which are very slow) in order to determine when to bubble up individual events. This means that reset is the only event that this module will emit, whenever something in the parent collection warrants this happening.

REST pagination

This module does not do REST pagination with the server, that is something that is done via ampersand-rest-collection.

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT