The default Viewflow interface is equipped with Hotwire/Turbo. This means that you should be cautious with JavaScript code initialization, as entire pages are updated with AJAX.
Viewflow suggests wrapping all code as standard browser WebElements, which automates the initialization as soon as a component is added to a page.
With numerous options available for setting up a JavaScript build pipeline, this guide focuses on using Vite as the build tool.
npm init -y
npm install vite --save-dev
"scripts": {
"vite": "vite build",
},
import { defineConfig } from 'vite'
export default defineConfig({
build: {
sourcemap: true,
emptyOutDir: false,
outDir: 'static/js/',
lib: {
entry: 'components/index.js',
formats: ['iife'],
name: 'my_components',
fileName: () => "my_components.min.js",
},
}
})
This configuration enables source maps, specifies the output directory, and defines the library settings for your components.
mkdir components/
touch components/index.js
npm run vite
This step confirms that your Vite configuration is correctly set up and ready for work.
Before diving into coding, it’s essential to configure the directories where Django will look for JavaScript and CSS files.
STATICFILES_DIRS = [BASE_DIR / "static"]
This tells Django to include the specified static/ directory when searching for static files.
After setting up your static directory, the next step is to incorporate your static files into Django templates.
To include the generated JavaScript file in your base_page.html template, modify the template as follows:
{% extends 'viewflow/base_page.html' %}{% load static %}
{% block extrahead %}
<script src="{% static 'js/my_components.min.js' %}" type="text/javascript"></script>
{% endblock %}
This snippet extends the base page template from Viewflow, loads Django’s static file handling mechanism, and includes your JavaScript file in the extrahead block, ensuring it’s loaded with the page.
To create a custom JavaScript component, you will start by defining the component in a new file within the components directory.
Create the file components/my_component.js and define your component as follows:
export class MyComponent extends HTMLElement {
connectedCallback() {
// ...
}
disconnectedCallback() {
// ...
}
}
The connectedCallback method is used for initializing the component or setting up event listeners when the component is added to the document. The disconnectedCallback method serves to clean up anything necessary when the component is removed, such as removing event listeners.
After creating your component, the next step is to include and register it so it can be used within your HTML files.
In your components/index.js file, import the MyComponent class and register it as a custom element:
import {MyComponent} from './my_component.js';
window.customElements.define('my-component', MyComponent);
By calling window.customElements.define, you register the new element ‘my-component’ with the browser’s Custom Elements registry, allowing you to use it as <my-component></my-component> in your HTML. This step is crucial for integrating custom JavaScript functionality with your web components.