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

backbone.collectionsubset

v0.1.1

Published

Create sub-collections of other collections and keep them in sync

Downloads

9

Readme

backbone.collectionsubset

Create sub-collections of other collections and keep them in sync.

tasks = new TaskCollection

# Create a subset of our main collection
today = tasks.subcollection
  filter: (task) -> task.isToday()
today.url = '/tasks/today'

# Create a new task in the main collection...
tasks.create id: 200, today: true

# ...and it's automatically pushed to all subsets
today.get(200) is tasks.get(200) # true

# Model changes are pushed upwards
today.get(200).destroy()

# Create models in subsets and they are pushed to the parent collection
today.create id: 201
today.get(201) is tasks.get(2001) # true

# And they will update on fetch
tasks.fetch()

# When subsets are fetched, all the models are pushed to the parent collection
today.fetch()

Getting Started

Download the production version or the development version.

In your web page:

<script src="dist/backbone.collectionsubset.min.js"></script>

Now you have two methods for creating subsets (or subcollections).

Collection::subcollection

The easiest way to create a subset is to use Collection::subcollection

var TaskCollection = Backbone.Collection.extend();
var tasks = new TaskCollection;
var today = tasks.subcollection({
  filter: function(task) {
    return task.isToday();
  }
});

today will be an instance of tasks.constructor, in this case, TaskCollection.

Backbone.CollectionSubset

If you need access to the subset object itself (you might want to modify the filter) you can create a subset directly.

var allTasks = new Backbone.Collection;
var subset = new Backbone.CollectionSubset({
  parent: allTasks,
  filter: function(task) {
    return task.isToday();
  }
});

var todayTasks = subset.child;

subset.setFilter(function(model){
  return task.isYesterday();
});

Documentation

Depending on how you create the subset you have different interface. Creating a subcollection via Backbone.Collection::subcollection you will only have access to the child collection and the Backbone.CollectionSubset instance is completely transparent. However, you may need some extra flexibility so you can also create Backbone.CollectionSubset instances manually

subset = new Backbone.CollectionSubet parent: tasks

vs.

new_collection = tasks.subcollection
  filter: (model) -> model.isToday()

Options

  • refresh - Whether to refresh the collection from the parent immediately. Defaults to true
  • triggers - Set which model attribute changes to watch for. This is important for performance. Defaults to null
  • filter - The default filter. By default lets all models pass
  • child - The collection to use for the child. This isn't required and will be automatically created. Defaults to null
  • parent - required The collection to base the subset on. Must be a Backbone.Collection.

Methods

If you create a Backbone.CollectionSubset instance directly you will have access to these methods.

setParent

Set the parent collection. Will unbind any events on the collection that the subset current has and setup all the events for syncing with the child collection

setChild

Same as the previous option, but with the child collection.

setFilter

Set the filter used on the parent collection. This does not automatically refresh the child collection.

refresh

Refreshes the child collection with models from the parent. Clears out the child collection and refilters the parent. Triggers a 'refresh' event on the child.

dispose

Clean up the subset. Removes the child collection completely and removes all events from the parent child. Triggers a dispose event on the subset.

Todo

  • Add url option to subcollection that will prepend the parent collection url

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Also, please don't edit files in the "dist" subdirectory as they are generated via grunt. You'll find source code in the "lib" subdirectory!

Release History

(Nothing yet)

License

Copyright (c) 2012 Anthony Short Licensed under the MIT license.