Skip to content

Commit 01a2b2f

Browse files
author
Andres Vargas
committed
Data Binding
1 parent 62a66ef commit 01a2b2f

14 files changed

+138
-33
lines changed

TODO.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* ask for how wire:model="parent.message" works ?

app/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
path('admin/', admin.site.urls),
99
path('', include("livewire.urls")),
1010
path('counter', TV.as_view(template_name="counter.html"), name="counter"),
11+
path('helloworld', TV.as_view(template_name="helloworld.html"), name="helloworld"),
1112
path('posts', posts , name='posts'),
1213
path('', TV.as_view(template_name="index.html"), name="index"),
1314
] + staticfiles_urlpatterns()

core/templates/counter.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{% extends "base.html" %}
22
{% load livewire_tags %}
33
{% block content %}
4-
4+
5+
<p>
6+
The basic example of livewire ... rock yeah!
7+
</p>
8+
59
{% livewire "counter" %}
610

711
{% endblock content %}

core/templates/counter.livewire.html

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<div style="text-align: center">
2+
3+
4+
25
<button class="button is-primary is-light is-large" wire:click="increment">+</button>
36
<h1 class="title" >{{ count }}</h1>
47
<button class="button is-primary is-light is-large" wire:click="decrement">-</button>

core/templates/helloworld.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends "base.html" %}
2+
{% load livewire_tags %}
3+
{% block content %}
4+
5+
6+
<p>
7+
Module trying to implement the
8+
9+
https://laravel-livewire.com/docs/properties
10+
11+
</p>
12+
13+
{# livewire 'helloworld' #}
14+
15+
16+
<hr>
17+
18+
<p>
19+
20+
Data Binding
21+
22+
</p>
23+
24+
{% livewire 'helloworld_databind' %}
25+
26+
27+
{% endblock content %}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
<h1>{{ message }}</h1>
3+
<!-- Will output "Hello World!" -->
4+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
<input wire:model="parent.message" type="text">
3+
4+
<h1>{{ message }}</h1>
5+
</div>

core/templates/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<li>
1212
<a href=" {% url 'posts' %}" >posts</a>
1313
</li>
14+
<li><a href="{% url 'helloworld' %}"> hello world </a></li>
1415
</ul>
1516
</div>
1617
{% endblock content %}

core/templates/posts.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{% extends "base.html" %}
22
{% load livewire_tags %}
33
{% block content %}
4-
4+
5+
<p>
6+
Implementing the https://laravel-livewire.com/docs/mount-method
7+
8+
</p>
9+
510
{% livewire "posts" posts=posts %}
611

712
{% endblock content %}

core/views.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ def posts(request):
1010
}
1111
return render(request, "posts.html", context)
1212

13-
14-
1513
class PostsLivewire(LivewireComponent):
1614

1715
def mount(self, **kwargs):
@@ -30,8 +28,13 @@ def decrement(self, *args):
3028
def increment(self, *args):
3129
self.count +=1
3230

33-
def mount(self, **kwargs):
34-
return {
35-
'count': self.count
36-
}
31+
32+
33+
class HelloworldLivewire(LivewireComponent):
34+
message = "Hellowwwww mundo!"
35+
36+
37+
class HelloworldDatabindLivewire(LivewireComponent):
38+
message = "Hellowwwww mundo!"
39+
3740

fly.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
app = "livewire"
2+
3+
4+
[[services]]
5+
internal_port = 8080
6+
protocol = "tcp"
7+
8+
[services.concurrency]
9+
hard_limit = 25
10+
soft_limit = 20
11+
12+
[[services.ports]]
13+
handlers = ["http"]
14+
port = "80"
15+
16+
[[services.ports]]
17+
handlers = ["tls", "http"]
18+
port = "443"
19+
20+
[[services.tcp_checks]]
21+
interval = 10000
22+
timeout = 2000

livewire/utils.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@
1010
import importlib
1111
import htmlement
1212
import xml.etree.ElementTree as ET
13+
import re
14+
15+
16+
def get_vars(instance):
17+
props = set()
18+
for prop in dir(instance):
19+
if not callable(getattr(instance, prop)) and not prop.startswith("__"):
20+
props.add(prop)
21+
return props
1322

1423

1524
def get_id():
1625
return ''.join(random.choice(string.ascii_lowercase) for i in range(20))
1726

27+
28+
def snakecase(name):
29+
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
30+
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
31+
32+
1833
def get_component_name(name):
1934
name = name.replace("_", " ")
20-
return name.title().replace(" ","")
35+
return name.title().replace(" ", "")
36+
2137

2238
def instance_class(component_name, **kwargs):
23-
path = getattr(settings,"LIVEWIRE_COMPONENTS_PREFIX")
39+
path = getattr(settings, "LIVEWIRE_COMPONENTS_PREFIX")
2440
if not path:
2541
assert False, "LIVEWIRE_COMPONENTS_PREFIX missing"
2642
module = importlib.import_module(path)
2743
class_name = get_component_name(component_name)
2844
class_livewire = getattr(module, '{}Livewire'.format(class_name))
2945
inst = class_livewire(**kwargs)
3046
return inst
31-
32-

livewire/views.py

+35-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.shortcuts import render
21
from django.template.loader import render_to_string
32
from django.utils.encoding import smart_str
43
from django.utils.safestring import mark_safe
@@ -11,6 +10,7 @@
1110
import htmlement
1211
import xml.etree.ElementTree as ET
1312
from .utils import get_id, get_component_name, instance_class
13+
from .utils import get_vars, snakecase
1414

1515
def livewire_message(request, component_name):
1616
inst = instance_class(component_name)
@@ -21,30 +21,41 @@ def livewire_message(request, component_name):
2121

2222

2323
class LivewireComponent(object):
24-
id = None
24+
__id = None
25+
2526

2627
def __init__(self, **kwargs):
27-
self.kwargs = kwargs
28+
self.__kwargs = kwargs
2829

2930
def get_component_name(self):
30-
name = self.__class__.__name__.lower()
31-
return name.replace("livewire","")
31+
name = self.__class__.__name__.replace("Livewire","")
32+
name = snakecase(name)
33+
return name
3234

3335
def get_dom(self):
3436
context = self.get_context()
3537
return self._render_component(context)
3638

39+
def fill(self, context): # Livewire Compatility https://laravel-livewire.com/docs/properties
40+
self.update_context(context)
41+
3742
def get_context(self):
38-
kwargs = self.kwargs
39-
return self.mount(**kwargs)
43+
kwargs = self.__kwargs
44+
mount_result = {}
45+
if hasattr(self, "mount"): # Livewire Compatility
46+
mount_result = self.mount(**kwargs)
47+
params = get_vars(self)
48+
for property in params:
49+
mount_result[property] = getattr(self, property)
50+
return mount_result
4051

4152
def get_response(self):
4253
dom = self.get_dom()
4354
return {
44-
'id': self.id,
55+
'id': self.__id,
4556
'name': self.get_component_name(),
4657
'dom': dom,
47-
'fromPrefetch':'',
58+
'fromPrefetch': '',
4859
'redirectTo': '',
4960
'children': [],
5061
'dirtyInputs': [],
@@ -59,20 +70,20 @@ def update_context(self, data_context):
5970
context = self.get_context()
6071
context.update(data_context)
6172
for key, value in context.items():
62-
setattr(self, key,value)
73+
setattr(self, key, value)
6374
return context
6475

6576

6677
def parser_payload(self, payload):
67-
self.id = payload.get("id")
78+
self.__id = payload.get("id")
6879
self.update_context(payload.get("data"))
6980
action_queue = payload.get("actionQueue", [])
7081
for action in action_queue:
7182
action_type = action.get("type")
7283
payload = action.get("payload")
7384
if action_type == "callMethod":
7485
method = payload.get("method")
75-
params = payload.get("params",[])
86+
params = payload.get("params", [])
7687
"""
7788
TODO:
7889
RUN THIS IT IS REALLY SAFE ???
@@ -81,37 +92,40 @@ def parser_payload(self, payload):
8192
"""
8293
local_method = getattr(self, method)
8394
local_method(*params)
95+
elif action_type == "syncInput":
96+
data = {}
97+
data[payload["name"]] = payload["value"]
98+
self.update_context(data)
8499

85100
def render(self):
86101
response = self.get_response()
87102
return response
88103

89-
def _render_component(self, context, initial_data={} ):
104+
def _render_component(self, context, initial_data={}):
90105
component_name = self.get_component_name()
91106
component_render = render_to_string(f'{component_name}.livewire.html',
92107
context=self.get_context())
93108
root = htmlement.fromstring(component_render).find("div")
94-
root.set('wire:id', self.id)
109+
root.set('wire:id', self.__id)
95110
if initial_data:
96-
root.set("wire:initial-data",json.dumps(initial_data))
111+
root.set("wire:initial-data", json.dumps(initial_data))
97112
res = ET.tostring(root)
98-
return mark_safe(smart_str(res))
99-
113+
return mark_safe(smart_str(res))
100114

101115
def render_initial(self):
102-
self.id = get_id()
116+
self.__id = get_id()
103117
component = self.get_component_name()
104118
context = self.get_context()
105119
initial_data = {
106-
'id': self.id,
120+
'id': self.__id,
107121
'name': component,
108122
'redirectTo': False,
109123
"events": [],
110124
"eventQueue": [],
111125
"dispatchQueue": [],
112-
"data": context,
126+
"data": context,
113127
"children": {},
114-
"checksum": "9e4c194bb6aabf5f1" # TODO: checksum
128+
"checksum": "9e4c194bb6aabf5f1" # TODO: checksum
115129
}
116130
return self._render_component(context, initial_data=initial_data)
117131

requirements.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
django
22
django-bsync
33
django-seed
4+
stringcase

0 commit comments

Comments
 (0)