function VirtualdomCached(data) { this.data = data; function renderOne(d) { return { tag: 'div', key: d.id, //class: ['one', 'two', (d.id%2 ? 'three' : '')], attributes: {'data-id': d.id, class: 'one two' + (d.id%2 ? ' three' : '')}, children: [ {tag: 'span'/*, class: ['user']*/, attributes: {'data-id': d.user.id, class: 'user'}, children: [d.user.name]}, {tag: 'span'/*, class: ['text']*/, attributes: {class: 'text'}, children: [d.text]} ] }; } try { // ES6 ftw var cache = this.cache = new Map(); } catch (e) { return this; } function cached(d) { var cached = cache.get(d); if (!cached) { cached = renderOne(d); cache.set(d, cached); } return cached; } this.render = (function () { var data = this.data.data; return {tag: 'div', class: ['testcase'], children: data.map(cached)}; }).bind(this); this.vdom = this.render(); this.el = v.toDOM(this.vdom); }
function Virtualdom(data) { this.data = data; function renderOne(d) { return { tag: 'div', //class: ['one', 'two', (d.id%2 ? 'three' : '')], attributes: {'data-id': d.id, class: 'one two' + (d.id%2 ? ' three' : '')}, children: [ {tag: 'span'/*, class: ['user']*/, attributes: {'data-id': d.user.id, class: 'user'}, children: [d.user.name]}, {tag: 'span'/*, class: ['text']*/, attributes: {class: 'text'}, children: [d.text]} ] }; } this.render = (function () { var data = this.data.data; return {tag: 'div', class: ['testcase'], children: data.map(renderOne)}; }).bind(this); this.vdom = this.render(); this.el = v.toDOM(this.vdom); }