-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvdom-view.js
35 lines (31 loc) · 1023 Bytes
/
vdom-view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
var Backbone = require('backbone');
var diff = require('virtual-dom/diff');
var patch = require('virtual-dom/patch');
var createElement = require('virtual-dom/create-element');
var VNode = require('virtual-dom/vnode/vnode');
var VText = require('virtual-dom/vnode/vtext');
var convertHTML = require('html-to-vdom')({
VNode: VNode,
VText: VText
});
module.exports = Backbone.View.extend({
setElement: function() {
Backbone.View.prototype.setElement.apply(this, Array.prototype.slice.call(arguments));
this.virtualEl = convertHTML(this.el);
this.virtualEl.tagName = this.el.tagName;
this.internalEl = createElement(this.virtualEl);
this.$el.html(this.internalEl);
return this;
},
render: function() {
var newVirtualEl = convertHTML(this.renderEl());
var patches = diff(this.virtualEl, newVirtualEl);
this.internalEl = patch(this.internalEl, patches);
this.virtualEl = newVirtualEl;
return this;
},
renderEl: function() {
return this.el;
}
});