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.collectionview

v0.1.1

Published

A view optimized for rendering collections

Downloads

8

Readme

Backbone.CollectionView

A view optimized for rendering collections. Give the CollectionView a collection and a view to render each item with and it will take care of updating the view with the collection.

  • Whenever a model is added or removed from a collection it will automatically be added or removed from the view.
  • It will set loading and ready states as well as showing and hiding fallback content if there are no items in the list.
  • If a model is added to a specific point in a collection it will render the item at the correct spot in the list
  • It will clean up after itself so there are no memory leaks

Getting Started

Download the production version or the development version.

In your web page:

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

Create a CollectionView and pass it the collection and the item view.

// Our collection of tasks
var tasks = new Backbone.Collection();

// Create a view for a task list item
var TaskListItemView = Backbone.View.extend({
  template: 'templates/task'
});

// Create a collection view. Tell it which view to
// use for rendering the items. Alternatively this can be passed
// into the options when creating a collectionview instance.
var TaskListView = Backbone.CollectionView.extend({
  itemView: taskListItemView
}); 

// Now create an instance of the CollectionView
var todolist = new TaskListView({
  collection: tasks
});

// Render it on the page
todolist.render();
todolist.renderAllItems();

// Add an item to the list and the collection
// Listens for the 'add' event and creates a view
tasks.create({ 
  title: 'Pick up milk'
});

// Shortcut to directly create a model in the collection
// via the collection view. 
todolist.add({
  title: 'Eat some bacon'
});

Documentation

The first thing you need to know about creating a collection view is that it needs a view for the items and a collection.

Setting the item view

The item view is the view that is used to render each of the models in the collection. eg. In a list of tasks the item view is the view for a single task item. Item views will have access to the model and the collection by default.

You can set the item view in a few ways:

1. As an instance property

var TaskListView = Backbone.CollectionView.extend({
  itemView: myItemView
});

2. In the options

var TaskListView = Backbone.CollectionView.extend();
var list = new TaskListView({
  itemView: myItemView
}); 

You can use this to override the view set in the original definition as well. This allows you to use the same collection view for different types of lists that have the same functionality. For example, a list of albums in a library may have a cover view and a title view.

3. Override getItemView method

If you want to do some processing on the view before it's used then you can override the getItemView method.

var TaskListView = Backbone.CollectionView.extend({
  getItemView: function(model){
    return new myItemView({
      model: model
    }); 
  }
});

Options

  • render - Automatically render when an instance is created
  • renderItems - Automatically render the items. This will also call render
  • itemView - The view to use to render each item in the collection
  • collection - required The collection whose models will be rendered.

After Item Render Hook

There is a method named afterRenderItem that will be fired whenever an item is rendered in the list.

afterRenderItem: function(view, model) {}

You can override this method if you need to do some extra processing on views after they are rendered.

Item View Events

You can easily listen for events on the views in the collection too. Just add a viewEvents object to your collection view. This is handy for things like selections.

var TaskListItem = Backbone.View.extend({
  events: {
    'click': 'select'
  },
  select: function(event) {
    this.trigger('select', this);
  }
});

var TaskListView = Backbone.CollectionView.extend({
  itemView: TaskListItem,
  viewEvents: {
    'select': 'onItemSelect'
  },
  initialize: function(options) {
    this.selected = [];
  },
  onItemSelect: function(view) {
    this.selected.push(view);
  }
});

Collection Events

You can also listen for events on the collection easilt as well.

var TaskListView = Backbone.CollectionView.extend({
  itemView: TaskListItem,
  collectionEvents: {
    'sort': 'onCollectionSort'
  },
  onCollectionSort: function(view) {
    # Do sorting stuff
  }
});

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 "src" subdirectory!

Release History

0.1.0 - First release

License

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