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

snbinder

v0.0.3

Published

SNBinder is a client-side template engine implemented in JavaScript. Just like server-side template engines, it binds a view (HTML template) to a data (JavaScript object) and generate an HTML text.

Downloads

4

Readme

SNBinder is a client-side template engine implemented in JavaScript. Just like server-side template engines, it binds a view (HTML template) to a data (JavaScript object) and generate an HTML text.

  1. Design Principle and Sample Application

I developed SNBinder with a belief that the client-side data-binding gives a great flexibility to developers who want to offer a "desktop-application-like" user experience. Read the following article if you are interested in the architecture behind this effort.

http://www.facebook.com/note.php?note_id=179569622077334

Fruence (a groupware application for Facebook users) is the showcase application that demonstrates the user experience enabled by this architecture.

http://www.fruence.com

  1. Initialization

SNBinder requires JQuery. JQuery must be loaded before SNBinder.

After loading both JQuery and SNBinder, the application should initialize SNBinder by calling it's init method like this:

$(document).ready(function() {
    SNBinder.init({});
}

The init method takes an optional parameter, which is described in the "Advanced Initialization" section below.

  1. Binding

To bind an HTML template to a JavaScript object, you need to call SNBinder.bind() method. For example,

var template = "<p>Hello $(.name)!</p>";
var user = { "name":"Leonardo da Vinci" };
$('.body').html(SNBinder.bind(template, user));

will replace the contents of the body tag with "Hello Leonardo da Vinci!".

If you want to apply the same template to multiple objects, it's more efficient to use a complied form.

var template = "<p>Hello $(.name)!</p>";
var apply_template = SNBinder.compile(template);
var user1 = { "name":"Leonardo da Vinci" };
var user2 = { "name":"Michelangelo di Lodovico Buonarroti Simoni" };
$('.div#user1').html(apply_template(user1));
$('.div#user2').html(apply_template(user2));

It is also possible to bind a template to an array of objects:

var template = "<li>Hello $(.name)!</li>";
var users = [
    { "name":"Leonardo da Vinci" }, 
    { "name":"Michelangelo di Lodovico Buonarroti Simoni" }, 
    { "name":"LDonato di Niccolò di Betto Bard" }
];
$('.ul').html(SNBinder.bind_rowset(template, users);

Following patterns in the template will be replaced.

$(.foo) will be replaced by the value of property "foo" (escaped)
$(_foo) will be replaced by the value of property "foo" (non-escaped)
$(+foo) will be replaced by the value of property "foo" (urlencode)
$(index) will be replaced by the index (in case or bind_rowset)
  1. Loading templates

Although it is possible to hard-code HTML templates in JavaScript code like samples above, it is not a good practice to mix View and Controller (notice that JavaScript is activing as a Controller). SNBinder offers two helper functions that allows developers to load multiple templates in a single HTTP GET.

SNBinder.get_sections(url, callback)
SNBinder.get_named_sections(url, callback)

The load_sections method loads a template bundle (an array of templates joined with "{%}") from the specified URL, and calls the callback function with an array of templates.

The load_sections method loads a named template bundle (set of named templates, where each name is specified with "{%}...{%}"), and calls the callback function with a dictionary of templates. For example, assume the named template bundle has follwing contents (a single template named "main") and accessible at "/static/template.htm"

{%}main{%}
<p>Hello $(.name)!</p>

The following code will load this template bundle, and performs the same view-data binding described in section 2.

SNBinder.get_named_sections("/static/templates.htm", null, function(templates) {
    var user = { "name":"Leonardo da Vinci" };
    $('.body').html(SNBinder.bind(templates("main", user));
});
  1. Loading data via JSON over HTTP

SNBinder has a set of helper methods, which makes it easy to fetch data (Json objects) over HTTP.

SNBinder.get(url, params, isJson, callback, options);
SNBinder.post(url, params, isJson, callback);

url: url to get/post the data from/data
params: url parameters (JavaScript object)
isJson: true if the server returns a JSON data
callback: callback function that processes the data (if isJson is false) or json and data (if isJson is true)
options: optional parameters to control the cache (default is {bypass_cache:false, cache_result:true} )

For example, if "/user/info" returns the JSON object represents the user (such as {"name":"Leonardo da Vinci"}), the example in previous section will become something like this:

SNBinder.get_named_sections("/static/templates.htm", null, function(templates) {
    SNBinder.get("/user/info", null, true, function(user) {
        $('.body').html(SNBinder.bind(templates("main", user));
    });
});
  1. Cache control

SNBinder has an in-memory cache for data and templates fetched via get() method, and following methods allows the application to access and control the cache.

flush_all(): flush all the cached data
flush(url, params): flush associated with url + url parameters
  1. Advanced Initialization

If the application calls SNBinder.get or SNBinder.post with isJson=true and the server returns an JSON object that has the property "login_required" with true in it, SNBinder calls the "login" function specified in the optional parameter to the SNBinder.init() method.