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

v1.0.1

Published

VirtualDOM-aware Backbone View

Downloads

8

Readme

backbone.vdomview

This library provides a VirtualDOM-aware Backbone View, called Backbone.VDOMView.

It depends on snabbdom for the virtual-DOM implementation.

How to use

To use it, extend Backbone.VDOMView. Then, instead of implementing a render method in your view, add a toHTML method which returns the View's HTML as a string.

The HTML of the toHTML must be structured so that there's a root element containing everything else. This root element is the view's top-level element, in other words, it's the DOM node represented by the this.el or this.$el attribute of the View.

React has a similar requirement that JSX returned by a component's render method should have a root node which contains everything else.

The rest will then be handled by VDOMView, which will automatically generate a diff between the view's current DOM element and new virtual-DOM node and then patch the actual DOM with this diff.

For example:

const MyView = Backbone.VDOMView.extend({

    tagName: 'span',
    className: 'vdom-span',

    toHTML () {
        return this.template(_.assign(this.model.toJSON()));
    }
});

The toHTML method

One important difference between Backbone.VDOMView and Backbone.View that should be noted is that the HTML being rendered (in the case of Backbone.VDOMView this is done in the toHTML method) should include the root element of the view.

So in the example above toHTML should return <span class="vdom-span"> ... </span> as the outer part of the HTML string.

This is different from normal Backbone.View classes, where your template will only return the inner part of the view element.

Event registration on virtual nodes

Snabbdom implements non-core functionality in separate modules.

Backbone.VDOMView makes use of all Snabbdom's modules except for the eventlisteners module.

The eventlisteners module allows you to add event listeners when creating a virtual node via the h method.

However Backbone.VDOMView doesn't use the h method of Snabbdom at all (it doesn't even include the code for it). Instead, it expects you to render the HTML for the view in the toHTML method, for example by using an underscore or lodash template.

There's therefore no way to attach these event listeners.

This way of registering event listeners is also in contrast to Backbone's declarative way of registering events, which is more the "Backbone way".

Backbone.VDOMView will make sure that these declaratively registered event listeners will remain active whenever the View's DOM representation changes.

The beforeRender and afterRender lifecycle methods

Backbone.VDOMView will call two lifecycle methods (if they exist). These are beforeRender and afterRender and are respectively called before and after toHTML is called.

Using Backbone.VDOMView without jQuery

Backbone can be used without jQuery by using Backbone.NativeView instead of Backbone.View.

If Backbone.NativeView is available, then the VDOMView will use that instead of Backbone.View.


Backbone.VDOMView is used in converse.js.

If you have any questions, feel free to create an issue or contact me directly.