虛擬DOM和版本比較算法:virtual-dom

jopen 9年前發布 | 51K 次閱讀 算法 virtual-dom

virtual-dom是一個JavaScript DOM 模型 支持元素創建, 差異計算patch operations 來實現高效的重新渲染。

示例

var h = require('virtual-dom/h');
var diff = require('virtual-dom/diff');
var patch = require('virtual-dom/patch');
var createElement = require('virtual-dom/create-element');

// 1: Create a function that declares what the DOM should look like function render(count) { return h('div', { style: { textAlign: 'center', verticalAlign: 'center', lineHeight: (100 + count) + 'px', border: '1px solid red', width: (100 + count) + 'px', height: (100 + count) + 'px' } }, [String(count)]); }

// 2: Initialise the document var count = 0; // We need some app data. Here we just store a count.

var tree = render(count); // We need an initial tree var rootNode = createElement(tree); // Create an initial root DOM node ... document.body.appendChild(rootNode); // ... and it should be in the document

// 3: Wire up the update logic setInterval(function () { count++;

  var newTree = render(count);
  var patches = diff(tree, newTree);
  rootNode = patch(rootNode, patches);
  tree = newTree;

}, 1000);</pre>

項目主頁:http://www.baiduhome.net/lib/view/home/1418607332183

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!