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-virtual-collection

v0.6.16

Published

Use Backbone.Marionette CollectionViews on a subset of a Backbone collection

Downloads

7,013

Readme

Backbone VirtualCollection

Build Status

Backbone VirtualCollection allows you display a subset of a Backbone collection in a Backbone view that updates in real time. It works great with Marionette CollectionViews.

If you're thinking "why don't you just place the models you're interested in a new collection?" the answer is that the new collection won't update itself when new models are added to the original collection, so you're creating data inconsitency. That's why we need a VirtualCollection.

Installation

a) with npm npm install backbone-virtual-collection

b) or manually add backbone.virtual-collection.js directly to your web app.

Usage

For example, let's say you have a task collection, and want to show a list of tasks that belong to a specific user.

We can instantiate a virtual collection that only contains tasks that belong to Rupert (who has user_id 13). The constructor takes two parameters, the first is the parent collection, the second is a options object. The filter option specifies a function that takes the model as argument. You can also just specify a hash of attributes to match.

var virtual_collection = new VirtualCollection(tasks_collection, {
  filter: function (task) {
    return task.get('user_id') == 13;
  }
});
// or using a hash of attributes to match
virtual_collection = new VirtualCollection(tasks_collection, {
  filter: {
    user_id: 13
  }
});

var view = new TaskListView({
  collection: virtual_collection
});

The Marionette collection view will only display the tasks that belong to Rupert, and it will update automatically. In other words, when a task is created that belongs to Rupert it will appear, but not if it belongs to Bob.

Sorting

Be default, the virtual collection will have the same sorting order as the parent collection. However, a comparator can be specified to change this. The comparator behaves like a Backbone comparator. In other words, you can specify a function or the name of an attribute to sort by.

var virtual_collection = new VirtualCollection(tasks_collection, {
  filter: { user_id: 13 },
  comparator: 'name'
});
// tasks in the virtual_collection will be sorted by name

You can also change the sorting order on the fly.

virtual_collection.comparator = 'created_at';
virtual_collection.sort(); // triggers sort event
// virtual_collection is now sorted by date, but the parent collection has not changed

Unbinding

The virtual collection will keep listening to its parent collection until you call stopListening.

You can pass a destroy_with option when creating the virtual collection being that an event emitter. The virtual collection will stop listening to events when the destroy_with event emitter emits a destroy event.

var virtual_collection = new Backbone.VirtualCollection(collection, {
  filter: {foo: 'bar'},
  destroy_with: view
});

Note: Prior to Marionette 2.*, "destroy" was called "close". For compatibility with older versions of Marionette, the old option close_with is still available, handling the close event.

Update filter

It's very common that you'd want to update the filter being used and have the collection view update itself. updateFilter takes the same parameters as the original filter option (a hash, or a function) and regenerates the virtual collection without losing your view bindings.


virtual_collection.updateFilter({
  the_new: 'properties'
, are: 'lovely'
});

// or

virtual_collection.updateFilter(function (model) {
  return model.foo() === 'bar';
});

Philosophy

No data duplication

VirtualCollection does not store, or duplicate any data. We've used other solutions in the past, and duplicating data is just plain bad news.

It's Fast

VirtualCollection maintains an internal index of models that pass the filter. That way, using the accessors and iterators (map, each, etc) is fast. It doesn't have to go through the whole parent collection and re-evaluate all the filters.

Happy hacking!

Changelog - License

ఠ ͟ಠ Pull requests are welcome, naturally