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-subview-manager

v0.1.2

Published

Simple subview manager for Backbone

Downloads

3

Readme

Backbone Subview-Manager

Greenkeeper badge

Minimalist mixin and abstract view implementation to manager subviews in your Backbone application.

Installation

  • With npm: npm install backbone-subview-manager --save.
  • With bower: bower install backbone-subview-manager --save.

Once installed, import the library using ES6 import, or import the ES5 script (transpiled with babel):

<script type="text/javascript" src="/vendors/backbone-subview-manager/dist/es5/backbone-subview-manager.js"></script>

View

Here is an example:

import Backbone from 'backbone';
import {CompositeView} from 'backbone-subview-manager';
import {TodoCollection} from './todo.collection';
import {TodoView} from './todo.view';

export class MyView extends CompositeView {
  initialize() {
    this.collection = new TodoCollection();
    this.listenToOnce(this.collection, 'sync', this.render);
    this.collection.fetch();
  }

  tagName() {
    return 'ul';
  }

  render() {
    this.removeSubViews();
    this.addSubViews(this.collection.map((todo) => {
      const todoView = new TodoView({
        model: todo,
      });

      todoView.render();
      this.$el.append(todoView.$el);
      return todoView;
    }));
  }
}

What happens here?

  1. Each element in the collection is rendered in its own view.
  2. Once a view is appened, it is automatically registered using the addSubView method.
  3. When the main is removed, each subviews will be automatically destroyed (using their remove method) to avoid memory leaks.

And that's all!

Note that you can also call initSubView to create the view automatically. The following code is exactly the same as the previous one:

import Backbone from 'backbone';
import {CompositeView} from 'backbone-subview-manager';
import {TodoCollection} from './todo.collection';
import {TodoView} from './todo.view';

export class MyView extends CompositeView {
  initialize() {
    this.collection = new TodoCollection();
    this.listenToOnce(this.collection, 'sync', this.render);
    this.collection.fetch();
  }

  tagName() {
    return 'ul';
  }

  render() {
    this.removeSubViews();
    this.collection.forEach((todo) => {
      const todoView = this.initSubView(TodoView, {
        model: todo,
      }));

      todoView.render();
      this.$el.append(todoView.$el);
    });
  }
}

API

The following methods can be used on any CompositeView:

[this] addSubViews(views)

Register subview or array of subview that will be automatically removed when the parent view is removed.

[view] initSubView(ViewImpl, options)

Create the subview (using options as constructor parameter), and register it using the addSubView method.

[this] removeSubViews(views)

Remove the subview, or array of subviews (the subview may be the view instance or the view cid). Calling this function without parameters will remove all subviews.

History

This little library has been created in 2013 (still used in production) and open sourced in 2016 after a rewrite in ES6.

License

MIT.