@daz4126/helium 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/helium.js +6 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Here's a simple example of a button that counts the number of times it has been
|
|
|
12
12
|
|
|
13
13
|
It's really simple to use - just sprinkle the magic @attributes into your HTML and watch it come alive!
|
|
14
14
|
|
|
15
|
-
[See more examples here](https://codepen.io/daz4126/pen/
|
|
15
|
+
[See more examples here](https://codepen.io/daz4126/pen/ogxGMX)
|
|
16
16
|
|
|
17
17
|
## Why Helium?
|
|
18
18
|
|
|
@@ -367,6 +367,8 @@ You can add modifiers by appending them with a dot (`.`) after the event name:
|
|
|
367
367
|
|
|
368
368
|
Helium includes built-in support for making HTTP requests directly from event handlers. This makes it easy to load data, submit forms, and update parts of your page without writing fetch code.
|
|
369
369
|
|
|
370
|
+
[See some examples here](https://codepen.io/daz4126/pen/dPMVGdX)
|
|
371
|
+
|
|
370
372
|
### Available HTTP Methods
|
|
371
373
|
|
|
372
374
|
- `@get` - GET request
|
|
@@ -463,6 +465,17 @@ Specifies the request parameters. Can be:
|
|
|
463
465
|
```
|
|
464
466
|
|
|
465
467
|
**Shorthand Syntax:**
|
|
468
|
+
|
|
469
|
+
You can write the params in shorthand using `:` separated string of attributes:
|
|
470
|
+
|
|
471
|
+
```html
|
|
472
|
+
<!-- Creates { user: { name: [value] } } -->
|
|
473
|
+
<button @post="/api/save" @params="user:name:value" name="value">
|
|
474
|
+
Save
|
|
475
|
+
</button>
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**Magic Parmas Syntax:**
|
|
466
479
|
If the element has a `name` attribute, Helium automatically extracts its value:
|
|
467
480
|
|
|
468
481
|
```html
|
|
@@ -478,14 +491,6 @@ For checkboxes:
|
|
|
478
491
|
<button @post="/api/consent" name="agreed">Submit</button>
|
|
479
492
|
```
|
|
480
493
|
|
|
481
|
-
**Nested Object Syntax:**
|
|
482
|
-
```html
|
|
483
|
-
<!-- Creates { user: { name: [value] } } -->
|
|
484
|
-
<button @post="/api/save" @params="user:name:value" name="value">
|
|
485
|
-
Save
|
|
486
|
-
</button>
|
|
487
|
-
```
|
|
488
|
-
|
|
489
494
|
**FormData Example:**
|
|
490
495
|
```html
|
|
491
496
|
<form @post="/api/upload">
|
|
@@ -690,6 +695,8 @@ HTTP request functions that can be called programmatically:
|
|
|
690
695
|
</button>
|
|
691
696
|
```
|
|
692
697
|
|
|
698
|
+
The arguments are `url`,`params` (not for `$get`) and `options`. `options` is an object that can include the properties `loading`,`target`, `template`
|
|
699
|
+
|
|
693
700
|
### $refs
|
|
694
701
|
Object containing all elements marked with `@ref` (prefixed with `$`):
|
|
695
702
|
|
package/helium.js
CHANGED
|
@@ -34,7 +34,7 @@ window.helium = function() {
|
|
|
34
34
|
|
|
35
35
|
const update = (data,targets,actions,template) => {
|
|
36
36
|
const newTargets = [];
|
|
37
|
-
targets
|
|
37
|
+
targets?.forEach((target,i) => {
|
|
38
38
|
const element = target instanceof Node ? target : (HELIUM.refs.get(target.trim()) || $(target.trim()));
|
|
39
39
|
if(element){
|
|
40
40
|
const content = template ? template(data) : data;
|
|
@@ -46,7 +46,7 @@ window.helium = function() {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const ajax = (url,method,options={},params={}) => {
|
|
49
|
-
if(options.loading) options.target = update(options.loading,options.target,options.action) || options.target;
|
|
49
|
+
if(options.loading && options.target) options.target = update(options.loading,options.target,options.action) || options.target;
|
|
50
50
|
const fd = params instanceof FormData, token = document.querySelector('meta[name="csrf-token"]')?.content;
|
|
51
51
|
const path = new URL(url, window.location.href);
|
|
52
52
|
const sameOrigin = path.origin === window.location.origin;
|
|
@@ -57,7 +57,7 @@ fetch(url, {
|
|
|
57
57
|
...(!fd && method !== "GET" && {"Content-Type":"application/json"}),
|
|
58
58
|
...(sameOrigin && token ? {"X-CSRF-Token": token} : {})
|
|
59
59
|
},
|
|
60
|
-
body: method === "GET" ? null : (fd ? params : JSON.stringify(
|
|
60
|
+
body: method === "GET" ? null : (fd ? params : JSON.stringify(params)),
|
|
61
61
|
credentials: sameOrigin ? "same-origin" : "omit"
|
|
62
62
|
})
|
|
63
63
|
.then(res => {
|
|
@@ -278,9 +278,9 @@ function processElements(element) {
|
|
|
278
278
|
if (!mods.includes("outside") || !el.contains(e.target)) {
|
|
279
279
|
if (isHttpMethod) {
|
|
280
280
|
const getAttr = name => el.getAttribute(`data-he-${name}`) || el.getAttribute(`@${name}`);
|
|
281
|
-
const pairs =
|
|
282
|
-
const target = pairs
|
|
283
|
-
const action = pairs
|
|
281
|
+
const pairs = getAttr('target')?.split(",")?.map(p => p.split(":").map(s => s.trim()));
|
|
282
|
+
const target = pairs?.map(([target]) => target);
|
|
283
|
+
const action = pairs?.map(([, action]) => action);
|
|
284
284
|
const options = {
|
|
285
285
|
...(getAttr("options") && parseEx(getAttr("options") || "{}")),
|
|
286
286
|
...(target && { target }),
|