Skip to content

Commit 755c88b

Browse files
committed
feat: observe attributes if match found lazyload component
1 parent afd31a4 commit 755c88b

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ export function removeComponent(key) {
1111
function listen(callback, selector) {
1212

1313
function observerCallback({ target }) {
14-
let isInit = target.querySelector(selector)
15-
if (isInit) {
14+
// let isInit = target.querySelector(selector)
15+
// if (isInit) {
1616
callback()
1717
// console.log('lazyloaded', selector)
1818
observer.uninit(observerCallback)
19-
}
19+
// }
2020
}
2121

2222
observer.init({
@@ -26,7 +26,32 @@ function listen(callback, selector) {
2626
callback: observerCallback
2727
})
2828

29-
// todo: observer add attributes
29+
let selectorAttributes = [];
30+
let attributes = selector.split(",")
31+
for (let attribute of attributes){
32+
let attr = attribute.trim()
33+
if (attr.startsWith("[")) {
34+
let pos = attr.indexOf("*")
35+
if (pos == -1)
36+
pos = attr.indexOf("=")
37+
if (pos !== -1) {
38+
attr = attr.slice(1, pos)
39+
} else {
40+
attr = attr.slice(1, -1)
41+
}
42+
selectorAttributes.push(attr)
43+
}
44+
45+
}
46+
if (selectorAttributes.length > 0)
47+
observer.init({
48+
name: 'lazyloadAttributeObserver',
49+
observe: ['attributes'],
50+
attributeName: selectorAttributes,
51+
target: selector,
52+
callback: observerCallback
53+
});
54+
3055
}
3156

3257
export async function lazyLoad(name, selector, cb) {

0 commit comments

Comments
 (0)