• Stars
    star
    1,698
  • Rank 27,486 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Basic virtual-dom algorithm

simple-virtual-dom

build codecov.io Dependency Status npm version

js-standard-style

Simple virtual-dom algorithm. It has only ~500 lines of code, including very basic idea of virtual-dom algorithm.

NOTE: This is an experiment library, DO NOT use it in any serious productions.

Install

npm:

$ npm install simple-virtual-dom --save

or checkout the bundle.js in dist:

<script src="simple-virtual-dom/dist/bundle.js"></script>

Usage

var svd = require('simple-virtual-dom')

var el = svd.el
var diff = svd.diff
var patch = svd.patch

// 1. use `el(tagName, [propeties], children)` to create a virtual dom tree
var tree = el('div', {'id': 'container'}, [
    el('h1', {style: 'color: blue'}, ['simple virtal dom']),
    el('p', ['Hello, virtual-dom']),
    el('ul', [el('li')])
])

// 2. generate a real dom from virtual dom. `root` is a `div` element
var root = tree.render()

// 3. generate another different virtual dom tree
var newTree = el('div', {'id': 'container'}, [
    el('h1', {style: 'color: red'}, ['simple virtal dom']),
    el('p', ['Hello, virtual-dom']),
    el('ul', [el('li'), el('li')])
])

// 4. diff two virtual dom trees and get patches
var patches = diff(tree, newTree)

// 5. apply patches to real dom
patch(root, patches)

// now the `root` dom is updated

You can checkout the full example in example folder.

You should always provide a unique key property for each child in array(just like ReactJS's keyed children) for Virtual-DOM to reorder children instead of replacing the whole list when perform diff algorithm.

var root = el('ul', [
  el('li', {key: 'uid1'}, ['Jerry']),
  el('li', {key: 'uid2'}, ['Tomy']),
  el('li', {key: 'uid3'}, ['Lucy']),
])

var newRoot = el('ul', [
  el('li', {key: 'uid1'}, ['Jerry']),
  el('li', {key: 'uid2'}, ['Tomy']),
  el('li', {key: 'uid4'}, ['Lily']),
  el('li', {key: 'uid3'}, ['Lucy']),
])

// ensure `patches` is minimum
var patches = diff(root, newRoot)

License

The MIT License (MIT)

Copyright (c) 2015 Livoras

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

blog

Too young, too simple. Sometimes, naive.
Ruby
3,545
star
2

list-diff

Diff two lists in O(n).
JavaScript
176
star
3

MVW-demos

Sample Codes for Model-View-Whatever Patterns
JavaScript
104
star
4

nestscript

A script nested in JavaScript, dynamically run code in environment without `eval` and `new Function`.
JavaScript
82
star
5

virtual-template

Combining Template Engine and Virtual-DOM
JavaScript
76
star
6

wx-arduino

wx-arduino
Python
35
star
7

amini

Amini 是一个 Angular 风格的微信小程序开发库
TypeScript
22
star
8

feb

Simple Front-end Development Workflow
JavaScript
20
star
9

home

new home
JavaScript
20
star
10

backbone.Composite

A plugin for backbone.js. Use composite pattern for backbone's views.
JavaScript
14
star
11

interview

12
star
12

stereojs

for communication between browser‘s tabs
JavaScript
10
star
13

genea

Genetic Algorithm for JavaScript
JavaScript
6
star
14

de-mini

突破微信小程序限制,动态加载代码。
4
star
15

swagger-mocker

Create Mock API Server from Swagger 2.0 JSON file.
Go
4
star
16

QLearning

A simple QLeaning Agent in Golang
Go
3
star
17

feifanote

Extremely Pure Online Notebook for Everyone (Front-end).
JavaScript
3
star
18

nestscript-demo

使用 nestscript 编译的开源伪 3D 游戏
JavaScript
3
star
19

easydict

The simplest dictionary on Linux.
Python
3
star
20

ts-mixins

Mixins In TypeScript with Code Suggestions.
2
star
21

shortest-path-in-diagram

shortest-path-in-diagram
TypeScript
2
star
22

saber

Developing desktop like JavaScript Application integrating knockoutjs, jQuery and requirejs.
JavaScript
2
star
23

too-lazy

Too Lazy Too Write Markup Language.
TypeScript
2
star
24

shooting-chicken

Epic HTML5 Game By Livoras
JavaScript
2
star
25

go-web-utilities

Golang Web Utilities Leverage on Gin
Go
1
star
26

pauo

TypeScript Runtime base on Golang without V8
1
star
27

outlet.js

A Bridge Between JavaScript and HTML.
1
star
28

vue-boilerplate

A boilerplate for building large app with vuejs.
CoffeeScript
1
star
29

neuron

神经元实验
TypeScript
1
star
30

meta-draw

Framework for drawing everything.
1
star
31

easorm

Database without pain.
1
star
32

auto-ad

Python
1
star
33

min-app-store

Wechat Mini App Store
JavaScript
1
star
34

polygon-line-intersection

polygon-line-intersection
1
star
35

tween-scroller

jQuery Tween Scroller Plugin
CoffeeScript
1
star
36

iqld-ad-picture-gen

TypeScript
1
star
37

wechat-picture-slide

wechat-picture-slide
JavaScript
1
star
38

wxminiapp2vue

Parser wxml to angular template
TypeScript
1
star
39

ragejs

RageJS, a JavaScript MVVM framework.
JavaScript
1
star