Git Product home page Git Product logo

Comments (6)

imacrayon avatar imacrayon commented on May 27, 2024

Dang, I thought we had a test to catch this, it might be broken. I’ll look into it tomorrow.

from alpine-ajax.

DrSensor avatar DrSensor commented on May 27, 2024

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.

imacrayon avatar imacrayon commented on May 27, 2024

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.

DrSensor avatar DrSensor commented on May 27, 2024

I tried to add x-target and yup, it perform 2 requests

  1. GET /?username=John+Smith (missing URL path)
  2. 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.

DrSensor avatar DrSensor commented on May 27, 2024

That double request also happen when doing self-target #41

from alpine-ajax.

imacrayon avatar imacrayon commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.