Comments (6)
Dang, I thought we had a test to catch this, it might be broken. I’ll look into it tomorrow.
from alpine-ajax.
I just test the minimum example and it works.
Gosh, I'm not sure what's wrong with my code. Gonna close this.
Here is the full snapshot before I refactor it to use `@ajax:{before,after,success}`
<!DOCTYPE html>
<meta name=viewport content='width=device-width,height=device-height,initial-scale=1,user-scalable=no'>
<link rel=stylesheet href=//cdn.jsdelivr.net/npm/boltcss>
<script defer src=//cdn.jsdelivr.net/npm/@imacrayon/[email protected]/dist/cdn.min.js></script>
<script type=module>
import init, { qr_svg, SvgOptions, Shape } from "//cdn.jsdelivr.net/npm/[email protected]/fast_qr.min.js"
await init()
window.qrcode = passwd => URL.createObjectURL(new Blob([qr_svg(passwd, new SvgOptions()
.margin(4)
.shape(Shape.Square)
.image("")
.background_color("#b8a4e5")
.module_color("#ffffff"))], {type:"image/svg+xml"}));
</script>
<script defer src=//cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js></script>
<style>
@media print {
* {
display: none;
}
#fasilitator {
display: unset;
}
}
</style>
<h2>Fasilitator</h2>
<form x-data='{
form: { duration: {} }, prev: {},
complete: false, fill: false, check: false, print: false
}' x-effect='
complete = form.access && form.duration.complete
fill = form.access || form.duration.incomplete
check = (form.user !== prev.user) && !fill
if (print && form.user !== prev.user) print = false
' @submit='
$event.submitter.disabled = true
$ajax("/admin/fasilitator")
.then(() => print = true)
.finally(() => $event.submitter.disabled = false)
' x-target=fasilitator :method='check ? "GET" : "POST"'>
<label>Username
<input name=id x-model=form.user type=text required>
</label>
<label>Akses
<select name=access x-model=form.access>
<option hidden selected value>
<option value=1>Perencanaan
<option value=2>Pelaksanaan
<option value=3>Evaluasi
</select>
</label>
<fieldset x-data='{ start: null, end: null }' x-effect='
form.duration.complete = start && end
form.duration.incomplete = start || end
if (new Date(start) > new Date(end)) end = null
'>
<legend>Durasi</legend>
<label>Mulai
<input name=startAt :min='new Date().toLocaleDateString("sv")' x-model=start type=date>
</label>
<label>Berakhir
<input name=endAt :min='start ?? new Date().toLocaleDateString("sv")' x-model=end type=date>
</label>
</fieldset>
<button @click='
if (print) window.print()
prev.user = form.user
' :hidden=!form.user :disabled='form.user ? complete ? false : fill : true'
x-text='check ? "Cek" : print ? "Cetak" : "Perbarui/Daftarkan"'></button>
</form>
<table id=fasilitator></table>
from alpine-ajax.
I wonder if it could have something to do with that form using x-target
and @submit
together. x-target
adds it's own submit
handler, I think that could cause the form to fire off two ajax requests.
from alpine-ajax.
I tried to add x-target
and yup, it perform 2 requests
GET /?username=John+Smith
(missing URL path)GET /user
(missing URL params)
index.html
<!DOCTYPE html>
<script defer src=//cdn.jsdelivr.net/npm/@imacrayon/alpine-ajax/dist/cdn.min.js></script>
<script defer src=//cdn.jsdelivr.net/npm/alpinejs/dist/cdn.min.js></script>
<form x-init x-target=user method=GET @submit='
$event.submitter.disabled = true
$ajax("/user")
.finally(() => $event.submitter.disabled = false)
'>
<input type=text name=username value="John Smith">
<button>Submit</button>
</form>
<div id=user></div>
server.js
#!/bin/env -S deno run -A
import { serveFile } from "https://deno.land/std/http/file_server.ts";
async function handler(request) {
const params = new URL(request.url).searchParams;
if (params.size == 0) return serveFile(request, "./index.html");
return new Response(
`<div id=user>${params.get("username")}</div>`,
{
headers: {
"Content-Type": "text/html",
},
},
);
}
Deno.serve({ port: 8000 }, handler);
from alpine-ajax.
That double request also happen when doing self-target #41
from alpine-ajax.
I tried to work out a way to support mixing x-target
and $ajax
, but I couldn't find a pattern that worked without negative tradeoffs. In v0.6.0 $ajax
no long inherits settings from attributes, I think this implicit behavior was causing confusion for some newcomers. So you could structure the form example above to one of two ways:
With $ajax
:
<form x-init @submit="
$event.submitter.disabled = true
$ajax('/user', { target: 'user' })
.finally(() => $event.submitter.disabled = false)
">...</form>
Or with x-target
:
<form x-init method="get" action="/user" x-target="user">...</form>
I would recommend using x-target
in this case because it will automatically handler the $event.submitter.disabled
logic for you and it a bit more terse. Generally x-target
works for 80% of use cases and $ajax
is there when you need it.
from alpine-ajax.
Related Issues (20)
- Question on passing CSRF tags for Django HOT 1
- [BUG] x-merge=prepend,append,update does not seem to work HOT 3
- x-ref replace x-target HOT 15
- Bug when target is a tr
- TypeError in latest version HOT 2
- Able to set `enctype` to application/x-www-form-urlencoded HOT 1
- Add support for `autofocus`
- `MissingIdError` error thrown when using `x-target.replace` without an ID HOT 2
- Calling $ajax from code outside of Alpine.js HOT 5
- Use ajax:success to preload assets (js) HOT 3
- Could not find a declaration file for module '@imacrayon/alpine-ajax' HOT 3
- Any way to make incoming html replace the innerHTML of target instead of the whole target? HOT 2
- Uncaught (in promise) Missing Target: #[object HTMLButtonElement] was not found in the current document. HOT 5
- SSE and websockets support HOT 3
- Using x-morph, new Alpine components don't initialize HOT 2
- Apply different merge strategy per target HOT 4
- Polling support? HOT 2
- Dynamically assigning a value for x-target not working, but works if assigning it statically. HOT 4
- Filterable content + infinite scroll: how to set x-merge attribute HOT 3
- v.0.6.0 broke the usual x-target behavior HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.