Skip to content

Commit

Permalink
add embedded json as a valid source
Browse files Browse the repository at this point in the history
  • Loading branch information
thipages committed Oct 18, 2024
1 parent 9352c3b commit e87cb21
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,24 @@ data.json
{"data1": "data11", "data2":"data12"},
{"data2": "data21", "data2":"data22"}
]
```
### Source can be also an embedded json

```html
<script type="application/json" id="embedded-data">
[
{
"id": 1,
"name": "ename1"
},
{
"id": 2,
"name": "ename2"
}
]
</script>
<list-m
template="a-template"
source="embedded-data"
></list-m>
```
21 changes: 17 additions & 4 deletions esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,22 @@ async function init(that) {
that.data = data;
}
async function getSourceContent(source) {
try {
return await fetch$1(source).json()
} catch (e) {
return false
const el = document.getElementById(source);
if (el) {
try {
const data = JSON.parse(el.textContent);
console.log('data', data);
return data
} catch (e) {
return false
}

} else {
try {
return await fetch$1(source).json()
} catch (e) {
return false
}
}

}
2 changes: 1 addition & 1 deletion esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@ async function init(that) {
that.data = data
}
async function getSourceContent(source) {
try {
return await fetch(source).json()
} catch (e) {
return false
const el = document.getElementById(source)
if (el) {
try {
const data = JSON.parse(el.textContent)
console.log('data', data)
return data
} catch (e) {
return false
}

} else {
try {
return await fetch(source).json()
} catch (e) {
return false
}
}

}
16 changes: 16 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
<script type="module" src="./index.js"></script>
</head>
<body>
<script type="application/json" id="embedded-data">
[
{
"id": 1,
"name": "ename1"
},
{
"id": 2,
"name": "ename2"
}
]
</script>
<h1>list-m Tests</h1>
<div id="results">

Expand All @@ -35,5 +47,9 @@ <h1>list-m Tests</h1>
template="info-m"
source="./data.txt"
></list-m>
<list-m
template="info-m"
source="embedded-data"
></list-m>
</body>
</html>
6 changes: 4 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const allLists = [...document.getElementsByTagName('list-m')]
const descriptions = [
'test with json file',
'test with non existing file',
'test with text file'
'test with text file',
'test with embedded json'
]
const allExpected = [
'fname1,1,fname2,2,fname3,3',
'',
''
'',
'ename1,1,ename2,2'
]
setTimeout(run, 1000)
function run() {
Expand Down

0 comments on commit e87cb21

Please sign in to comment.