Skip to content

Commit

Permalink
Allow a single stream response to update multiple elements
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Jan 16, 2021
1 parent 178c817 commit 8bb56d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/core/streams/stream_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import { StreamElement } from "../../elements/stream_element"

export const StreamActions: { [action: string]: (this: StreamElement) => void } = {
append() {
this.targetElement?.append(this.templateContent)
this.targetElements?.forEach(e => e.append(this.templateContent))
},

prepend() {
this.targetElement?.prepend(this.templateContent)
this.targetElements?.forEach(e => e.prepend(this.templateContent))
},

remove() {
this.targetElement?.remove()
this.targetElements?.forEach(e => e.remove())
},

replace() {
this.targetElement?.replaceWith(this.templateContent)
this.targetElements?.forEach(e => e.replaceWith(this.templateContent))
},

update() {
if (this.targetElement) {
this.targetElement.innerHTML = ""
this.targetElement.append(this.templateContent)
if (this.targetElements) {
this.targetElements.forEach(e => e.innerHTML = "")
this.targetElements.forEach(e => e.append(this.templateContent))
}
}
}
6 changes: 3 additions & 3 deletions src/elements/stream_element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export class StreamElement extends HTMLElement {
this.raise("action attribute is missing")
}

get targetElement() {
get targetElements() {
if (this.target) {
return this.ownerDocument?.getElementById(this.target)
return this.ownerDocument?.querySelectorAll(`#${this.target}`)
}
this.raise("target attribute is missing")
}

get templateContent() {
return this.templateElement.content
return this.templateElement.content.cloneNode(true)
}

get templateElement() {
Expand Down

0 comments on commit 8bb56d7

Please sign in to comment.