Skip to content

Commit 979e920

Browse files
author
Andres Vargas
committed
implement the updatesQueryString partially
1 parent 3005efb commit 979e920

File tree

7 files changed

+94
-40
lines changed

7 files changed

+94
-40
lines changed

TODO.md

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
* unittest
12
* ask for how wire:model="parent.message" works ?
3+
* Updating The Query String
4+
*

app/urls.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
path('counter', TV.as_view(template_name="counter.html"), name="counter"),
1111
path('helloworld', TV.as_view(template_name="helloworld.html"),
1212
name="helloworld"),
13-
path('posts', posts , name='posts'),
13+
path('posts', posts, name='posts'),
1414
path('search-posts', TV.as_view(template_name="search_post.html"),
1515
name="search_post"),
16+
path('show-posts/', TV.as_view(template_name="show_post.html"),
17+
name="show_post"),
1618
path('', TV.as_view(template_name="index.html"), name="index"),
1719
] + staticfiles_urlpatterns()

core/templates/show_post.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "base.html" %}
2+
{% load livewire_tags %}
3+
{% block content %}
4+
5+
<h2 class="subtitle">
6+
Show post
7+
</h2>
8+
<hr>
9+
{% with post_id=1 %}
10+
{% livewire "show_post" post_id=post_id %}
11+
{% endwith %}
12+
13+
{% endblock content %}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div>
2+
3+
<h1 class="title">{{post.title}}</h1>
4+
5+
<p class="content">
6+
{{post.content}}
7+
</p>
8+
9+
<div>
10+
<button class="button is-danger" wire:click="delete_post">Delete</button>
11+
12+
</div>
13+
14+
</div>

core/views.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,30 @@ class HelloworldDatabindLivewire(LivewireComponent):
4343

4444
class SearchPostsLivewire(LivewireComponent):
4545
search = ""
46-
46+
updates_query_string = ("search", )
47+
# TODO must recive the request
4748
def mount(self, **kwargs):
4849
posts = Post.objects.all()
4950
if self.search:
50-
print("search:" + self.search)
51-
posts = posts.filter(Q(title__icontains=self.search) | \
52-
Q(content__icontains=self.search))
53-
print("c: {}".format(posts.count()))
51+
param = Q(title__icontains=self.search) | Q(content__icontains=self.search)
52+
posts = posts.filter(param)
53+
return {
54+
'posts': list(posts.values("id", "title", "content")),
55+
'search': self.search # TODO: before mount we must put the new value of the search for updates_query_string works and work on the url
56+
}
57+
58+
59+
class ShowPostLivewire(LivewireComponent):
60+
post_id = None
5461

62+
63+
def mount(self, **kwargs):
64+
post_id = kwargs.get("post_id")
65+
post = Post.objects.get(id=post_id)
66+
self.post_id = post_id
5567
return {
56-
'posts': list(posts.values("id", "title", "content"))
68+
'post': {'title': post.title, 'content': post.content}
5769
}
5870

71+
72+

livewire/templatetags/livewire_tags.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def livewire_scripts(context):
88
return context
99

10-
@register.simple_tag
11-
def livewire(component,**kwargs):
10+
@register.simple_tag(takes_context=True)
11+
def livewire(context, component,**kwargs):
1212
livewire_component = instance_class(component, **kwargs)
1313
return livewire_component.render_initial()

livewire/views.py

+39-31
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515

1616
log = logging.getLogger(__name__)
1717

18+
1819
def livewire_message(request, component_name):
1920
inst = instance_class(component_name)
2021
if request.method == "POST":
21-
body = json.loads(request.body)
22-
inst.parser_payload(body)
23-
return JsonResponse(inst.render(), safe=False)
22+
inst.parser_payload(request)
23+
return JsonResponse(inst.render(request), safe=False)
2424

2525

2626
class LivewireComponent(object):
2727
__id = None
28-
28+
__request = None
2929

3030
def __init__(self, **kwargs):
3131
self.__kwargs = kwargs
3232

3333
def get_component_name(self):
34-
name = self.__class__.__name__.replace("Livewire","")
34+
name = self.__class__.__name__.replace("Livewire", "")
3535
name = snakecase(name)
3636
return name
3737

@@ -40,7 +40,9 @@ def get_dom(self):
4040
log.debug(context)
4141
return self._render_component(context)
4242

43-
def fill(self, context): # Livewire Compatility https://laravel-livewire.com/docs/properties
43+
def fill(
44+
self, context
45+
): # Livewire Compatility https://laravel-livewire.com/docs/properties
4446
self.update_context(context)
4547

4648
def get_context(self):
@@ -49,26 +51,29 @@ def get_context(self):
4951
params = get_vars(self)
5052
for property in params:
5153
mount_result[property] = getattr(self, property)
52-
if hasattr(self, "mount") and callable(self.mount): # Livewire Compatility
54+
if hasattr(self, "mount") and callable(self.mount): # Livewire Compatility
5355
mount_result = self.mount(**kwargs)
5456
return mount_result
5557

56-
def get_response(self):
58+
def get_response(self): # TODO: chnge to use render method on component view
5759
dom = self.get_dom()
58-
return {
59-
'id': self.__id,
60-
'name': self.get_component_name(),
61-
'dom': dom,
62-
'fromPrefetch': '',
63-
'redirectTo': '',
64-
'children': [],
65-
'dirtyInputs': [],
66-
'data': self.get_context(),
67-
'eventQueue': [],
68-
'dispatchQueue': [],
69-
'events': [],
70-
'checksum': "c24"
60+
json_response = {
61+
"id": self.__id,
62+
"name": self.get_component_name(),
63+
"dom": dom,
64+
"fromPrefetch": "",
65+
"redirectTo": "",
66+
"children": [],
67+
"dirtyInputs": [],
68+
"data": self.get_context(),
69+
"eventQueue": [],
70+
"dispatchQueue": [],
71+
"events": [],
72+
"checksum": "c24",
7173
}
74+
if hasattr(self, "updates_query_string"):
75+
json_response.update({'updatesQueryString': self.updates_query_string})
76+
return json_response
7277

7378
def update_context(self, data_context):
7479
context = self.get_context()
@@ -78,7 +83,9 @@ def update_context(self, data_context):
7883
setattr(self, key, value)
7984
return context
8085

81-
def parser_payload(self, payload):
86+
def parser_payload(self, request):
87+
self.__request = request
88+
payload = json.loads(request.body)
8289
self.__id = payload.get("id")
8390
action_queue = payload.get("actionQueue", [])
8491
for action in action_queue:
@@ -101,16 +108,18 @@ def parser_payload(self, payload):
101108
data[action_payload["name"]] = action_payload["value"]
102109
self.update_context(data)
103110

104-
def render(self):
111+
# TODO: chnge to use render method on component view
112+
def render(self, request):
105113
response = self.get_response()
106114
return response
107115

108116
def _render_component(self, context, initial_data={}):
109117
component_name = self.get_component_name()
110-
component_render = render_to_string(f'{component_name}.livewire.html',
111-
context=context)
118+
component_render = render_to_string(
119+
f"{component_name}.livewire.html", context=context
120+
)
112121
root = htmlement.fromstring(component_render).find("div")
113-
root.set('wire:id', self.__id)
122+
root.set("wire:id", self.__id)
114123
if initial_data:
115124
root.set("wire:initial-data", json.dumps(initial_data))
116125
res = ET.tostring(root)
@@ -121,15 +130,14 @@ def render_initial(self):
121130
component = self.get_component_name()
122131
context = self.get_context()
123132
initial_data = {
124-
'id': self.__id,
125-
'name': component,
126-
'redirectTo': False,
133+
"id": self.__id,
134+
"name": component,
135+
"redirectTo": False,
127136
"events": [],
128137
"eventQueue": [],
129138
"dispatchQueue": [],
130139
"data": context,
131140
"children": {},
132-
"checksum": "9e4c194bb6aabf5f1" # TODO: checksum
141+
"checksum": "9e4c194bb6aabf5f1", # TODO: checksum
133142
}
134143
return self._render_component(context, initial_data=initial_data)
135-

0 commit comments

Comments
 (0)