Skip to content

Commit

Permalink
Pass morpher settings into Component.init.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Oct 2, 2023
1 parent d3f618d commit c32f5c3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
13 changes: 10 additions & 3 deletions django_unicorn/static/unicorn/js/unicorn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from "./component.js";
import { isEmpty, hasValue } from "./utils.js";
import { components, lifecycleEvents } from "./store.js";
import { getMorpher } from "./morpher.js";

let messageUrl = "";
let csrfTokenHeaderName = "X-CSRFToken";
Expand All @@ -12,10 +13,15 @@ let morpher;
*
* @typedef
*/
export function init(_messageUrl, _csrfTokenHeaderName, _csrfTokenCookieName, _morpher) {
window.morpher = _morpher;
export function init(
_messageUrl,
_csrfTokenHeaderName,
_csrfTokenCookieName,
_morpherSettings
) {
messageUrl = _messageUrl;
morpher = _morpher;

morpher = getMorpher(_morpherSettings);

if (hasValue(_csrfTokenHeaderName)) {
csrfTokenHeaderName = _csrfTokenHeaderName;
Expand All @@ -24,6 +30,7 @@ export function init(_messageUrl, _csrfTokenHeaderName, _csrfTokenCookieName, _m
if (hasValue(_csrfTokenCookieName)) {
csrfTokenCookieName = _csrfTokenCookieName;
}

return {
messageUrl,
csrfTokenHeaderName,
Expand Down
8 changes: 2 additions & 6 deletions django_unicorn/templates/unicorn/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
<script src="{% static 'unicorn/js/unicorn.min.js' %}"></script>
<script>
const url = "{% url 'django_unicorn:message' %}";

const morpherSettings = JSON.parse(document.getElementById("unicorn:settings:morpher").textContent);
const morpher = getMorpher(morpherSettings);

Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpher);
Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpherSettings);

</script>
{% else %}
Expand All @@ -22,10 +20,8 @@
window.Unicorn = Unicorn;

const url = "{% url 'django_unicorn:message' %}";

const morpherSettings = JSON.parse(document.getElementById("unicorn:settings:morpher").textContent);
const morpher = getMorpher(morpherSettings);

Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpher);
Unicorn.init(url, "{{ CSRF_HEADER_NAME }}", "{{ CSRF_COOKIE_NAME }}", morpherSettings);
</script>
{% endif %}
5 changes: 4 additions & 1 deletion example/www/templates/www/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#menu li:first-child {
border-left: none !important;
}

</style>

<script defer src="https://unpkg.com/@alpinejs/[email protected]/dist/cdn.min.js"></script>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
{% unicorn_scripts %}
</head>

Expand Down Expand Up @@ -50,4 +53,4 @@ <h1>django-unicorn</h1>
</main>
</body>

</html>
</html>
18 changes: 1 addition & 17 deletions tests/js/unicorn/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@ import test from "ava";
import { init } from "../../../django_unicorn/static/unicorn/js/unicorn.js";

test("init unicorn", (t) => {
const actual = init("unicorn/", "X-Unicorn", "unicorn");

t.true(actual.messageUrl === "unicorn/");
t.true(actual.csrfTokenHeaderName === "X-Unicorn");
t.true(actual.csrfTokenCookieName === "unicorn");
});

test("init unicorn with no reload", (t) => {
const actual = init("unicorn/", "X-Unicorn", "unicorn", false);

t.true(actual.messageUrl === "unicorn/");
t.true(actual.csrfTokenHeaderName === "X-Unicorn");
t.true(actual.csrfTokenCookieName === "unicorn");
});

test("init unicorn with reload", (t) => {
const actual = init("unicorn/", "X-Unicorn", "unicorn", true);
const actual = init("unicorn/", "X-Unicorn", "unicorn", { NAME: "morphdom" });

t.true(actual.messageUrl === "unicorn/");
t.true(actual.csrfTokenHeaderName === "X-Unicorn");
Expand Down

0 comments on commit c32f5c3

Please sign in to comment.