Skip to content

Commit 4aa99be

Browse files
author
Colt Pini
committed
created a lazyload param:
- removed the getdata call in the constructor to allow for lazyloading the data. - moved the getdata call into the handledata function to consolidate the calls and make the function a little simpler. - removed self = this to make the load function simpler - when the lazyload prop is set it will defer loading until the get() function is called. This will allow lazy loading to be called by what means needed, either a button click or if the element comes into the viewport. This will allow this component to remain simple and prefer composition to bloat. - fixed the tests to run async and cleared DOM after each test. - ticked the version number to a minor update.
1 parent 81d26fc commit 4aa99be

File tree

4 files changed

+142
-68
lines changed

4 files changed

+142
-68
lines changed

include-fragment-element.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function fire(name, target) {
1010
}, 0)
1111
}
1212

13-
function handleData(el, data) {
14-
return data.then(
13+
function handleData(el) {
14+
return getData(el).then(
1515
function(html) {
1616
const parentNode = el.parentNode
1717
if (parentNode) {
@@ -44,10 +44,6 @@ function getData(el) {
4444
export class IncludeFragmentElement extends HTMLElement {
4545
constructor() {
4646
super()
47-
// Preload data cache
48-
getData(this)['catch'](function() {
49-
// Ignore `src missing` error on pre-load.
50-
})
5147
}
5248

5349
static get observedAttributes() {
@@ -73,26 +69,34 @@ export class IncludeFragmentElement extends HTMLElement {
7369
}
7470
}
7571

72+
get lazyload() {
73+
return this.hasAttribute('lazyload')
74+
}
75+
set lazyload(val) {
76+
if (val) {
77+
this.setAttribute('lazyload', '')
78+
} else {
79+
this.removeAttribute('lazyload')
80+
}
81+
}
82+
7683
get data() {
7784
return getData(this)
7885
}
7986

8087
attributeChangedCallback(attribute) {
81-
if (attribute === 'src') {
82-
// Reload data load cache.
83-
const data = getData(this)
84-
88+
if (attribute === 'src' && !this.lazyload) {
8589
// Source changed after attached so replace element.
8690
if (this._attached) {
87-
handleData(this, data)
91+
handleData(this)
8892
}
8993
}
9094
}
9195

9296
connectedCallback() {
9397
this._attached = true
94-
if (this.src) {
95-
handleData(this, getData(this))
98+
if (this.src && !this.lazyload) {
99+
handleData(this)
96100
}
97101
}
98102

@@ -116,43 +120,40 @@ export class IncludeFragmentElement extends HTMLElement {
116120
}
117121

118122
load() {
119-
const self = this
120-
121123
return Promise.resolve()
122-
.then(function() {
123-
const request = self.request()
124-
fire('loadstart', self)
125-
return self.fetch(request)
124+
.then(() => {
125+
fire('loadstart', this)
126+
return this.fetch(this.request())
126127
})
127-
.then(function(response) {
128+
.then(response => {
128129
if (response.status !== 200) {
129130
throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`)
130131
}
131-
132132
const ct = response.headers.get('Content-Type')
133133
if (!ct || !ct.match(/^text\/html/)) {
134134
throw new Error(`Failed to load resource: expected text/html but was ${ct}`)
135135
}
136-
137136
return response
138137
})
139-
.then(function(response) {
140-
return response.text()
141-
})
138+
.then(response => response.text())
142139
.then(
143-
function(data) {
144-
fire('load', self)
145-
fire('loadend', self)
140+
data => {
141+
fire('load', this)
142+
fire('loadend', this)
146143
return data
147144
},
148-
function(error) {
149-
fire('error', self)
150-
fire('loadend', self)
145+
error => {
146+
fire('error', this)
147+
fire('loadend', this)
151148
throw error
152149
}
153150
)
154151
}
155152

153+
get() {
154+
handleData(this)
155+
}
156+
156157
fetch(request) {
157158
return fetch(request)
158159
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "include-fragment-element",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"main": "dist/index-umd.js",
55
"license": "MIT",
66
"repository": {

0 commit comments

Comments
 (0)