html-element.jsView |
---|
4 | 4 … | var walk = require('./lib/walk') |
5 | 5 … | var watch = require('./watch') |
6 | 6 … | var caches = new global.WeakMap() |
7 | 7 … | var watcher = null |
| 8 … | +var invalidateNextTick = require('./lib/invalidate-next-tick') |
8 | 9 … | |
9 | 10 … | module.exports = function (tag, attributes, children) { |
10 | 11 … | return Element(global.document, null, tag, attributes, children) |
11 | 12 … | } |
58 | 59 … | } else if (isObservable(node)) { |
59 | 60 … | var nodes = getNodes(document, resolve(node)) |
60 | 61 … | nodes.forEach(append, target) |
61 | 62 … | data.targets.set(node, nodes) |
62 | | - data.bindings.push(new Binding(bind, document, node, data)) |
| 63 … | + data.bindings.push(new Binding(document, node, data)) |
63 | 64 … | } else { |
64 | 65 … | node = getNode(document, node) |
65 | 66 … | target.appendChild(node) |
66 | 67 … | walk(node, rebind) |
100 | 101 … | } |
101 | 102 … | } |
102 | 103 … | } |
103 | 104 … | |
104 | | -function bind (document, obs, data) { |
105 | | - return watch(obs, function (value) { |
106 | | - var oldNodes = data.targets.get(obs) |
107 | | - var newNodes = getNodes(document, value) |
108 | | - if (oldNodes) { |
109 | | - replace(oldNodes, newNodes) |
110 | | - data.targets.set(obs, newNodes) |
111 | | - } |
112 | | - }) |
113 | | -} |
114 | | - |
115 | 105 … | function indexOf (target, item) { |
116 | 106 … | return Array.prototype.indexOf.call(target, item) |
117 | 107 … | } |
118 | 108 … | |
216 | 206 … | function resolve (source) { |
217 | 207 … | return typeof source === 'function' ? source() : source |
218 | 208 … | } |
219 | 209 … | |
220 | | -function Binding (fn, document, obs, data) { |
| 210 … | +function Binding (document, obs, data) { |
221 | 211 … | this.document = document |
222 | 212 … | this.obs = obs |
223 | 213 … | this.data = data |
224 | | - this.fn = fn |
225 | 214 … | this.bound = false |
| 215 … | + this.invalid = false |
| 216 … | + this.update = function (value) { |
| 217 … | + var oldNodes = data.targets.get(obs) |
| 218 … | + var newNodes = getNodes(document, value) |
| 219 … | + if (oldNodes) { |
| 220 … | + replace(oldNodes, newNodes) |
| 221 … | + data.targets.set(obs, newNodes) |
| 222 … | + } |
| 223 … | + } |
| 224 … | + invalidateNextTick(this) |
226 | 225 … | } |
227 | 226 … | |
228 | 227 … | Binding.prototype = { |
229 | 228 … | bind: function () { |
230 | 229 … | if (!this.bound) { |
231 | | - this._release = this.fn(this.document, this.obs, this.data) |
| 230 … | + this._release = this.invalid |
| 231 … | + ? watch(this.obs, this.update) |
| 232 … | + : this.obs(this.update) |
| 233 … | + this.invalid = false |
232 | 234 … | this.bound = true |
233 | 235 … | } |
234 | 236 … | }, |
235 | 237 … | unbind: function () { |
236 | 238 … | if (this.bound && typeof this._release === 'function') { |
237 | 239 … | this._release() |
238 | 240 … | this._release = null |
239 | 241 … | this.bound = false |
| 242 … | + invalidateNextTick(this) |
240 | 243 … | } |
241 | 244 … | } |
242 | 245 … | } |