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

nice

v0.7.1

Published

Component-based (instead of template-based) views for CoffeeScript

Downloads

37

Readme

nice

Component-based (instead of template-based) views for CoffeeScript.

Template-based views abandon all the powerful capabilities of programming languages, particularly the ability to reuse code. And they force us to learn a new template language instead of using one we already know.

Of course, there are good reasons we use templates. One of them is the difficulty of mixing data (the markup) and code. CoffeeScript does a great job of this and opens the door to fine-grained component-based views.

Nice provides the foundation for building component-based views.

Status

Very early stages of development. Right now, Nice simply provides a way to generate HTML from CoffeeScript (or Javascript, but, honestly, it's too cumbersome to use that way). For example:

@div [
  @h1 novel.title
  @h2
    class: "byline"
    text: novel.author
  @p novel.synopsis
]

Of course, that by itself isn't all that interesting, although it's certainly more readable than straight HTML. The really useful bit comes in when you start defining classes and methods that use this approach:

class ComponentHTML extends HTML

  list: (list) ->
    @ul (@li @a item for item in list)

Here we've defined an list method that will take an array of objects and gives us back the HTML for a list of hyperlinks. We can now reuse that in other classes or even override it.

Let's suppose we want to encapsulate all the code related to displaying information about novels. We might define a class like this:

class NovelHTML extends ComponentHTML

  description: (novel) ->
    @div [
      @h1 novel.title
      @h2
        class: "byline"
        text: novel.author
      @p novel.synopsis
      @div
        class: "characters"
        content: @characterList novel.characters
    ]

  characterList: (names) ->
    @list ({text: name, url: "novel://qubit/characters/#{name}"} for name in names)

We define characterList to be a wrapper around the list method above. Then we use that method within our description method.

So now we can replace templates with simple method invocations:

novel = 
  title: "QUBIT"
  author: "Dan Yoder"
  synopsis: "When the first practical quantum computer is developed, an ambitious criminal exploits to steal trillions of dollars."
  characters: ["Ulysses Mercy","Vihaan Malhotra","Katya Brittain","Ray Austin"]

novelHTML = new NovelHTML

console.log beautify (novelHTML.description novel), 
  indent_size: 2
  indent_char: ' '
  max_char: 78

Which will give us the following HTML:

<div>
  <h1>QUBIT</h1>
  <h2 class='byline'>Dan Yoder</h2>
  <p>When the first practical quantum computer is developed, an ambitious criminal
    exploits to steal trillions of dollars.</p>
  <div class='characters'>
    <ul class='menu'>
      <li><a href='novel://qubit/characters/Ulysses Mercy'>Ulysses Mercy</a>
      </li>
      <li><a href='novel://qubit/characters/Vihaan Malhotra'>Vihaan Malhotra</a>
      </li>
      <li><a href='novel://qubit/characters/Katya Brittain'>Katya Brittain</a>
      </li>
      <li><a href='novel://qubit/characters/Ray Austin'>Ray Austin</a>
      </li>
    </ul>
  </div>
</div>

Future Plans

The next step is to build on this foundation to make it possible to add behaviors, such as making elements of a list draggable, using mixins. Since we can easily convert HTML strings like this into DOM trees with JQuery, we can write component-classes that use the HTML classes to generate DOM trees and then bind behaviors to them.

We can also do the equivalent of compilation by simply memo-izing the methods.

Finally, dynamic updating can be handled by simply adding methods at the component level (not the HTML level) that operate directly on the DOM. But they can potentially reuse the same code that generated the view in the first place. (This is part of what we mean by "fine-grained").

For example, if we have a method that defines how we are going to render a list element, we can use that code to generate the initial HTML, and then reuse it in the code that does in-place updates of the list.