mirror of
https://github.com/Django-LiveView/docs.git
synced 2025-11-25 06:05:17 +01:00
docs: add debounce documentation
Some checks failed
Gitea Actions Deploy / deploy (push) Has been cancelled
Some checks failed
Gitea Actions Deploy / deploy (push) Has been cancelled
Added documentation for the new data-liveview-debounce attribute introduced in django-liveview 2.1.0. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
39
one.org
39
one.org
@@ -631,6 +631,45 @@ Execute functions when elements are first rendered:
|
|||||||
</div>
|
</div>
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
|
** Debounce
|
||||||
|
|
||||||
|
Reduce server calls by adding a delay before sending requests. Perfect for search inputs and real-time validation:
|
||||||
|
|
||||||
|
#+BEGIN_SRC html
|
||||||
|
<input
|
||||||
|
type="search"
|
||||||
|
name="search"
|
||||||
|
data-liveview-function="search_articles"
|
||||||
|
data-liveview-debounce="500"
|
||||||
|
data-action="input->page#run"
|
||||||
|
placeholder="Search articles...">
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
The ~data-liveview-debounce="500"~ attribute waits 500ms after the user stops typing before sending the request. This dramatically reduces server load and provides a better user experience.
|
||||||
|
|
||||||
|
**Example: Real-time search with debounce**
|
||||||
|
|
||||||
|
#+BEGIN_SRC python
|
||||||
|
from liveview import liveview_handler, send
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
|
@liveview_handler("search_articles")
|
||||||
|
def search_articles(consumer, content):
|
||||||
|
query = content["form"]["search"]
|
||||||
|
articles = Article.objects.filter(title__icontains=query)
|
||||||
|
|
||||||
|
html = render_to_string("search_results.html", {
|
||||||
|
"articles": articles
|
||||||
|
})
|
||||||
|
|
||||||
|
send(consumer, {
|
||||||
|
"target": "#search-results",
|
||||||
|
"html": html
|
||||||
|
})
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Without debounce, typing "python" would send 6 requests (one per letter). With ~data-liveview-debounce="500"~, it sends only 1 request after the user stops typing for 500ms.
|
||||||
|
|
||||||
** Middleware System
|
** Middleware System
|
||||||
|
|
||||||
Add middleware to run before handlers for authentication, logging, or rate limiting:
|
Add middleware to run before handlers for authentication, logging, or rate limiting:
|
||||||
|
|||||||
1538
one.org.backup
Normal file
1538
one.org.backup
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user