Skip to content

Commit 5cc2a6c

Browse files
committed
Undo underscore naming convention
1 parent bf8f1f7 commit 5cc2a6c

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

include-fragment-element.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
/* eslint-disable github/no-then */
22

3+
const privateData = new WeakMap()
4+
5+
function fire(name, target) {
6+
setTimeout(function() {
7+
const event = target.ownerDocument.createEvent('Event')
8+
event.initEvent(name, false, false)
9+
target.dispatchEvent(event)
10+
}, 0)
11+
}
12+
13+
function handleData(data, target) {
14+
return data.then(
15+
function(html) {
16+
const parentNode = target.parentNode
17+
if (parentNode) {
18+
target.insertAdjacentHTML('afterend', html)
19+
parentNode.removeChild(target)
20+
}
21+
},
22+
function() {
23+
target.classList.add('is-error')
24+
}
25+
)
26+
}
27+
328
export class IncludeFragmentElement extends HTMLElement {
429
constructor() {
530
super()
6-
this._privateData = new WeakMap()
731
// Preload data cache
8-
this._getData()['catch'](function() {
32+
this.getData()['catch'](function() {
933
// Ignore `src missing` error on pre-load.
1034
})
1135
}
1236

13-
_fire(name, target) {
14-
setTimeout(function() {
15-
const event = target.ownerDocument.createEvent('Event')
16-
event.initEvent(name, false, false)
17-
target.dispatchEvent(event)
18-
}, 0)
19-
}
20-
21-
_handleData(data) {
22-
return data.then(
23-
function(html) {
24-
const parentNode = this.parentNode
25-
if (parentNode) {
26-
this.insertAdjacentHTML('afterend', html)
27-
parentNode.removeChild(this)
28-
}
29-
}.bind(this),
30-
function() {
31-
this.classList.add('is-error')
32-
}.bind(this)
33-
)
34-
}
35-
3637
static get observedAttributes() {
3738
return ['src']
3839
}
@@ -56,50 +57,50 @@ export class IncludeFragmentElement extends HTMLElement {
5657
}
5758
}
5859

59-
_getData() {
60+
getData() {
6061
const src = this.src
61-
let data = this._privateData.get(this)
62+
let data = privateData.get(this)
6263
if (data && data.src === src) {
6364
return data.data
6465
} else {
6566
if (src) {
66-
data = this._load()
67+
data = this.load()
6768
} else {
6869
data = Promise.reject(new Error('missing src'))
6970
}
70-
this._privateData.set(this, {src, data})
71+
privateData.set(this, {src, data})
7172
return data
7273
}
7374
}
7475

7576
get data() {
76-
return this._getData()
77+
return this.getData()
7778
}
7879

7980
attributeChangedCallback(attribute) {
8081
if (attribute === 'src') {
8182
// Reload data load cache.
82-
const data = this._getData()
83+
const data = this.getData()
8384

8485
// Source changed after attached so replace element.
8586
if (this._attached) {
86-
this._handleData(data)
87+
handleData(data, this)
8788
}
8889
}
8990
}
9091

9192
connectedCallback() {
9293
this._attached = true
9394
if (this.src) {
94-
this._handleData(this._getData())
95+
handleData(this.getData(), this)
9596
}
9697
}
9798

9899
disconnectedCallback() {
99100
this._attached = false
100101
}
101102

102-
_request() {
103+
request() {
103104
const src = this.src
104105
if (!src) {
105106
throw new Error('missing src')
@@ -114,14 +115,14 @@ export class IncludeFragmentElement extends HTMLElement {
114115
})
115116
}
116117

117-
_load() {
118+
load() {
118119
const self = this
119120

120121
return Promise.resolve()
121122
.then(function() {
122-
const request = self._request()
123-
self._fire('loadstart', self)
124-
return self._fetch(request)
123+
const request = self.request()
124+
fire('loadstart', self)
125+
return self.fetch(request)
125126
})
126127
.then(function(response) {
127128
if (response.status !== 200) {
@@ -140,19 +141,19 @@ export class IncludeFragmentElement extends HTMLElement {
140141
})
141142
.then(
142143
function(data) {
143-
self._fire('load', self)
144-
self._fire('loadend', self)
144+
fire('load', self)
145+
fire('loadend', self)
145146
return data
146147
},
147148
function(error) {
148-
self._fire('error', self)
149-
self._fire('loadend', self)
149+
fire('error', self)
150+
fire('loadend', self)
150151
throw error
151152
}
152153
)
153154
}
154155

155-
_fetch(request) {
156+
fetch(request) {
156157
return fetch(request)
157158
}
158159
}

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const responses = {
4444
}
4545
}
4646

47-
window.IncludeFragmentElement.prototype._fetch = function(request) {
47+
window.IncludeFragmentElement.prototype.fetch = function(request) {
4848
const pathname = new URL(request.url).pathname
4949
return Promise.resolve(responses[pathname](request))
5050
}

0 commit comments

Comments
 (0)