Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
¿Cómo obtener datos solo si los datos no se completan en el front-end usando livewire y alpinejs?

Para usar menos recursos, quiero obtener datos del servidor (usando livewire) solo por primera vez y las otras veces para usar los datos ya obtenidos en alpinejs.

Entonces, si hay un cambio en mi <select> , se fillStates los estados de relleno:

 <select wire:model="selectedCountry" name="selectedCountry" id="selectedCountry" wire:change="fillStates"> <option value="">Select Country</option> @foreach($this->countries as $country) <option value="{{ $country->id }}">{{ $country->name }}</option> @endforeach </select>

El método fillStates devolverá los datos:

 public function fillStates() { $states = State::where('country_id', $this->selectedCountry)->get(); if(count($states)) { $this->states[$this->selectedCountry] = $states; return $this->states[$this->selectedCountry]; } return []; }

Y mi pregunta está aquí. ¿Cómo puedo evitar que solicite más de una vez cuando $this->states[$country] ya tiene valor y se puede acceder con alpinejs?

Por ejemplo, selecciono Estados Unidos en mi <select> . Por primera vez, necesita obtener los datos que son sus estados. Lo cambio a Canadá. Esta vez necesita recuperar los datos que son sus estados. Pero si vuelvo a seleccionar Estados Unidos, no debería solicitarlo, ya que ya se ha obtenido y está disponible en alpinejs. ¿Cómo puedo lograrlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que tienes que validar manualmente usando alpinejs.

 <div x-data="{ selectedCountry: null, countries: {}, }" x-init="$watch('selectedCountry', () => { if(! (selectedCountry in countries)) { @this.call('fillStates'); countries[selectedCountry] = @this.get('states'); } })" > <select x-model="selectedCountry" name="selectedCountry" id="selectedCountry" > <option value="">Select Country</option> @foreach($this->countries as $country) <option value="{{ $country->id }}">{{ $country->name }}</option> @endforeach </select> </div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!