An open API service indexing awesome lists of open source software.

https://github.com/mateusmaso/handlebars.binding

Handlebars plugin for using one-way data binding
https://github.com/mateusmaso/handlebars.binding

data-binding handlebars javascript

Last synced: 12 months ago
JSON representation

Handlebars plugin for using one-way data binding

Awesome Lists containing this project

README

          

handlebars.binding [![Build Status](https://travis-ci.org/mateusmaso/handlebars.binding.svg?branch=master)](https://travis-ci.org/mateusmaso/handlebars.binding)
==================

This is a Handlebars plugin which allows using one-way data binding inside templates with a clean markup. It saves development time by offering a simple and powerful way of building highly interactive templates without re-rendering or updating the DOM manually.

## Install

```
$ npm install --save handlebars.binding
```

## Usage

```javascript
var Handlebars = require("handlebars");
require("handlebars.binding").default(Handlebars);

var main = document.querySelector("main");
var context = {foo: 123};
var template = Handlebars.templates["path/to/template"];
var nodes = Handlebars.parseHTML(template(context));

nodes.forEach((node) => main.appendChild(node));

context.foo = 321;

Handlebars.update();
```

## Examples

### Binding with ```bind``` helper

```html

{{bind "foo"}}


Hello {{foo}}, {{bar}}


Hello {{foo}}, {{bar}}

{{#bind "foo"}}

Hello {{foo}}, {{bar}}


{{/bind}}
```

### Binding with ```if``` and ```unless``` helper

```html

{{if "foo" bind=true then="Hello" else="World"}}


Hello {{foo}}, {{bar}}


Hello {{foo}}, {{bar}}

{{#if "foo" bind=true}}

Hello {{foo}}, {{bar}}


{{else}}

Goodbye


{{/if}}
```

### Binding with ```each``` helper

```html
{{#each objects var="object" bind=true}}

Item {{index}}: {{object.name}}


{{object.content}}


{{/each}}

{{#each objects bind=true}}

Item {{index}}: {{name}}


{{content}}


{{/each}}

{{#each primitives var="primitive" bind=true}}

Item {{index}}: {{primitive}}


{{/each}}

{{#each primitives bind=true}}

Item {{index}}: {{$this}}


{{/each}}
```

### Unbinding and rebinding DOM

```javascript
Handlebars.unbind(node);
Handlebars.bind(node);
```

## License

MIT © [Mateus Maso](http://www.mateusmaso.com)