Skip to content

Commit

Permalink
Allow LiveElement to be used as a parent for LiveElement and LiveNode…
Browse files Browse the repository at this point in the history
…List
  • Loading branch information
molovo committed Feb 13, 2019
1 parent 2929301 commit 0587f43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/live-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default class LiveElement extends Observable {
constructor (selector, parent = document.documentElement) {
super(selector, parent)

this.item = this.parent.querySelector(this.selector)
if (this.parent) {
this.item = this.parent.querySelector(this.selector)
}

this.registerDOMObserver()
}
Expand Down
6 changes: 5 additions & 1 deletion src/live-node-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export default class LiveNodeList extends Observable {
constructor (selector, parent = document.documentElement) {
super(selector, parent)

this.items = this.parent.querySelectorAll(this.selector)
if (this.parent) {
this.items = this.parent.querySelectorAll(this.selector)
} else {
this.items = []
}

this.registerDOMObserver()
}
Expand Down
23 changes: 14 additions & 9 deletions src/observable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LiveElement } from './live-element'
import { bind } from 'decko'

export default class Observable {
Expand Down Expand Up @@ -59,7 +60,18 @@ export default class Observable {
*/
constructor (selector, parent = document.documentElement) {
this.selector = selector
this.parent = parent

if (parent instanceof this.constructor) {
this.parent = parent.item
parent.on('update', (newItem, oldItem) => {
this.pause()
this.parent = newItem
this.resume()
this.refresh()
})
} else {
this.parent = parent
}
}

/**
Expand Down Expand Up @@ -222,14 +234,7 @@ export default class Observable {
registerDOMObserver () {
this.observer = new MutationObserver(this.refresh)

if (this.parent) {
this.observer.observe(this.parent, {
attributes: false,
characterData: false,
childList: true,
subtree: true
})
}
this.resume()

this.events.start.forEach(callback => callback())
}
Expand Down

0 comments on commit 0587f43

Please sign in to comment.