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

stache-bind

v0.1.2

Published

Minimum viable mustache data binding.

Downloads

7

Readme

Mustache data binding

Simple data binding with Mustache templates.

Motivation

Web applications frequently need to reflect changes to model data in the view presented to the user. This is commonly done with many calls to document.querySelector, followed by attribute assignments.

// Model data.
const user = {name: 'Hubot', login: 'hubot', avatar: 'https://github.com/hubot.png'};

// Display the model in the view.
const link = document.querySelector('.avatar-link');
link.setAttribute('title', user.login);
link.setAttribute('href', user.url);

const image = link.querySelector('.avatar');
image.setAttribute('alt', user.login);
image.src = user.avatar;

When the user model data changes, all elements that depend on those values must be found and reassigned. This style becomes difficult to maintain as the number of templates and models grows. And design changes to the markup require a careful inspection of the corresponding JavaScript selectors to ensure they stay in sync.

So while an ad hoc solution works well as a starting point, we often want a reusable pattern as an app becomes more complex.

Data binding

One solution is to apply the Model-view-controller design pattern and bind the view (DOM nodes) as observers of model data. When the model changes, it notifies the observers, and updated values appear in the page.

This library provides one-way data binding: data flows from the model into the view, as in MVC. Some other frameworks implement two-way data binding, propagating changes in the view, typically from an <input> element or click event, directly back into the model.

One-way data binding prescribes no method of moving view changes into the model, but a representative controller looks like the following example.

// The controller in one-way data binding.
const input = document.querySelector('.name-input');
input.addEventListener('change', function(event) {
  // Update the model, transform data, notify other components, etc.
  user.name = input.value;
});

Usage

Store markup templates in the page with a unique data-name attribute.

<template data-name="avatar">
  <div class="avatar-container" data-id="{{ id }}" data-user="{{ user.login }}">
    <a class="avatar-link" href="{{ user.url }}" title="{{ user.login }}" target="_blank">
      <div class="avatar-frame">
        <img class="avatar photo" alt="{{ user.login }}" src="{{ user.avatar }}" height="230" width="230">
      </div>
      <h1 class="name">{{ user.name }}</h1>
    </a>
  </div>
</template>

Bind a model object—Mustache calls this a context—to a template.

import {template} from 'stache-bind';

const context = {
  id: 42,
  user: {
    name: 'Hubot',
    login: 'hubot',
    url: 'https://github.com/hubot',
    avatar: 'https://github.com/hubot.png?size=460'
  }
};

// Create a single template function using its data-name.
const avatar = template('avatar');

// Build a new DocumentFragment from the template, bound to the model data.
const fragment = avatar(context);

// Display the new fragment in the page.
document.body.appendChild(fragment);

// Sometime later the model data changes, and the view is updated.
context.user.name = 'Bender';

Any JavaScript object may be used as the template's rendering context.

Limitations

This is a minimally viable Mustache parser, and only the {{ }} stache syntax is currently supported. As browser support for Proxy improves, it can be used to observe Array mutations and implement the {{#}} {{/}} iterator section syntax.

Development

npm install
npm test

Alternatives

A sample of full-fledged JavaScript data binding frameworks.

License

Distributed under the MIT license. See LICENSE for details.