Giter VIP home page Giter VIP logo

unclejs's Introduction

Uncle.js

Uncle is a tiny (1.3k minzipped) virtual DOM library. Ya know React, right?

What's different:

  • Should be easy to understand because of it's small code base
  • HTML templates - no JSX
  • Templates can be precompiled for production (easy, TBD)

Live Demo on JSFiddle

Example

var TodoApp = {
  items: [
    {text: 'Buy milk', complete: false},
    {text: 'Rob a bank', complete: true}
  ],
  toggle: function(index) {
    this.items[index].complete = !this.items[index].complete;
    this.update();
  },
  // render each item in context of TodoApp
  render: uncle.render('<ul class="todo">{{ this.items.map(TodoItem, this)  }}</ul>'), 
  // mount our UL as a child of BODY
  update: uncle.update(document.body) 
};

var TodoItem = uncle.render('<li class="todo-{{complete? 'complete' : 'active'}}" onclick="this.toggle($index)">{{text}}<li>');

TodoApp.update();

Templates

Use double curly braces (mustaches) to embed Javascript one-liners.

<li class="todo-{{complete? 'complete' : 'active'}}">
  Item #{{$index}}: {{text}}
<li>

Special attributes (or "directives"):

  • key="unique{{id}}" - enables efficient reuse of DOM elements (as seen in React and other libraries)
  • onclick="this.method(event, arg1)" - DOM 1 event listeners where this is your render context (not a DOM element)
  • html="raw {{html}}" - same as innerHTML

API

uncle.render(html)

Converts your template to a Javascript function, which renders virtual DOM.

var HelloMessage = {
  name: "Mr. Spock",
  render: uncle.render("<div>Hello {{ this.name }}</div>")
};
// HelloMessage.render() == {tag: "div", attrs:{}, children:[ "Hello ", HelloMessage.name ]}

Resulting function accepts two optional arguments: render(some_value, index). This can be used with native Array methods like Array#map(). In template's context both arguments are avaliable as $value and $index accordingly. If some_value is an object, it's properties are made avaliable as regular variables for convenience.

uncle.update(containerElement)

Allows any "renderable" component to update the real DOM. When you call HelloMessage.update(), it runs this.render(), computes a diff between two virtual DOMs and patches the real one if needed.

HelloMessage.update = uncle.update(document.body);
HelloMessage.update(); 
// OR
var updateBody = uncle.update(document.body).bind(HelloMessage);
updateBody();

One can call update() on-demand or put it in a RAF loop like this:

function updateDOM() {
   HelloMessage.update();
   window.requestAnimationFrame(updateDOM);
}
updateDOM();

unclejs's People

Contributors

fedia avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.