TodoMVC Implementation with CFWheels and HTMX

March 29, 2022

Posted in Community, Tutorials

Posted By: Peter Amiri

<!-- wp:paragraph --> <p>Recently I've been playing around with HTMX and really starting to love it. So what is HTMX? From their <a href="https://htmx.org">website</a>:</p> <!-- /wp:paragraph --> <!-- wp:quote --> <blockquote class="wp-block-quote"><p>htmx gives you access to&nbsp;&nbsp;<a href="https://htmx.org/docs#ajax">AJAX</a>,&nbsp;<a href="https://htmx.org/docs#css_transitions">CSS Transitions</a>,&nbsp;&nbsp;<a href="https://htmx.org/docs#websockets">WebSockets</a>&nbsp;and&nbsp;<a href="https://htmx.org/docs#sse">Server Sent Events</a>&nbsp;directly in HTML, using&nbsp;<a href="https://htmx.org/reference#attributes">attributes</a>, so you can build&nbsp;<a href="https://htmx.org/examples">modern user interfaces</a>&nbsp;with the&nbsp;<a href="https://en.wikipedia.org/wiki/HATEOAS">simplicity</a>&nbsp;and&nbsp;<a href="https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">power</a>&nbsp;of hypertext</p><cite>Introduction to htmx</cite></blockquote> <!-- /wp:quote --> <!-- wp:paragraph --> <p>And also:</p> <!-- /wp:paragraph --> <!-- wp:quote --> <blockquote class="wp-block-quote"><p>Why should only&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a"><code>&lt;a&gt;</code></a>&nbsp;and&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form"><code>&lt;form&gt;</code></a>&nbsp;be able to make HTTP requests?<br>Why should only&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event"><code>click</code></a>&nbsp;&amp;&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event"><code>submit</code></a>&nbsp;events trigger them?<br>Why should only&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET"><code>GET</code></a>&nbsp;&amp;&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST"><code>POST</code></a>&nbsp;methods be&nbsp;<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods">available</a>?<br>Why should you only be able to replace the&nbsp;<strong>entire</strong>&nbsp;screen?<br></p><p>By removing these arbitrary constraints, htmx completes HTML as a&nbsp;<a href="https://en.wikipedia.org/wiki/Hypertext">hypertext</a></p><cite>Motivation behind htmx</cite></blockquote> <!-- /wp:quote --> <!-- wp:paragraph --> <p>So what does this all mean? Well, in its simplest form, it means being able to build modern web applications with the UX that users have come to expect, with the HTML, CSS, and the backend technology of your choice which in our case is CFML and CFWheels. </p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>So I decide to see if I could build the <a href="https://todomvc.com">TodoMVC</a> project using no hand written JavaScript and only relying on HTML, CSS, and CFWheels. I downloaded the template project and took a look at the application specs to get an idea of what to implement. </p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>Here is the video of the running app:</p> <!-- /wp:paragraph --> <!-- wp:image {"id":872,"sizeSlug":"full","linkDestination":"media"} --> <figure class="wp-block-image size-full"><a href="https://cfwheels.org/blog/wp-content/uploads/2022/03/cfwheels-todomvc-htmx.gif"><img src="https://cfwheels.org/blog/wp-content/uploads/2022/03/cfwheels-todomvc-htmx.gif" alt="" class="wp-image-872"/></a></figure> <!-- /wp:image --> <!-- wp:paragraph --> <p>So if you want to run the app locally, you'll need to have Commandbox installed and the CFWHeels CLI commands for CommandBox installed as well. With those two items taken care of, launch a CommandBox and issue the following commands.</p> <!-- /wp:paragraph --> <!-- wp:code --> <pre class="wp-block-code"><code>wheels g app name=todo datasourceName=todo template=cfwheels-todomvc-htmx --setupH2 package install server start</code></pre> <!-- /wp:code --> <!-- wp:paragraph --> <p>Let's look at those lines and talk about what they do. The first line <code>wheels g app</code> will download the template app from Forgbox.io and create a CFWheels application and name it <code>todo</code>. It also create a H2 database and configures the datasource for you. The next line will <code>install</code> all the dependencies of our app. These include, a few CommandBox modules to make development easier, the CFWheels core framework directory and place it into the <code>wheels</code> folder, and install the H2 drivers into our Lucee server for out application. The last line will <code>start</code> our Lucee server. I've also added a setting to automatically run the Database migrations on application startup so the database schema is created.</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>You can checkout the code on <a href="https://github.com/bpamiri/cfwheels-todomvc-htmx">GitHub</a>. Let me know what you think. </p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>EDIT: The Lucee server that starts up will have <code>cfwheels</code> set as its admin password.</p> <!-- /wp:paragraph -->
E
Ed

Hi Peter, Thanks for this example, I hadn't heard of htx before so I thought I would try it out. I followed the steps above but I'm getting an error message relating to h2 lucee.commons.lang.ClassException: cannot load class through its string name, because no definition for the class with the specified name [org.h2.Driver] could be found caused by (java.lang.ClassNotFoundException:org.h2.Driver;java.lang.ClassNotFoundException:org.h2.Driver not found by lucee.core [138];) Is this an issue with my installation of coldbox?

Mar 31, 2022

profile-picture
Peter Amiri

Ed that error basically means the H2 driver wasn't installed properly. I tried to take steps to make sure it gets installed automatically but sometimes it just doesn't like to play ball. The easiest way to solve this is to log into the Lucee administrator and go to the Extension section and click on Applications. Then select the H2 driver and install it. After you install it, restart Lucee. That should get it to work for you. I'll also look to see how I can make this install a little more robust.

Mar 31, 2022

profile-picture
Peter Amiri

Ed, I suspect the reason for this was because the Lucee server didn't get an admin password set on startup. That tends to stop the extension installation. I tend to forget about this step cause I have a file at ~/.box.env that sets a default admin password for all the Lucee servers I start up in CommandBox. I've added a default password of `cfwheels` that will get injected into the Lucee server on startup which will allow the H2 driver to be installed. I also tested this in a clean room MacOS VM and it worked. So give it another try by deleting the folder it created and issue the three commands again. Let me know how it goes. -Peter

Mar 31, 2022

profile-picture
Peter Amiri

Sorry you're having so much trouble but I really want to work through this to make the installation process more robust. So the first issue is the space in directory names. That would be in the CFWHeels CLI. Let me take a look at that quickly.

Mar 31, 2022

E
Ed

No need to apologise, I have time and it will help the community for this to be working. I know from experience that having a second pair of eyes and someone to beta test can help a lot :-)

Mar 31, 2022

profile-picture
Peter Amiri

Okay Ed, I just published a new version of the CLI. Can you issue a `box install cfwheels-cli` to see if it installs the v0.7.2 version. This should handle the spaces in directory paths better. Let me run this whole thing in a Windows PC to see how it behaves. I've been too Mac oriented lately. -Peter

Mar 31, 2022

E
Ed

Hi Peter, I tried the CLI update but when I tried the todo install I got the same error as before. I'm wondering if my update to v0.7.2 worked correctly, is there a way to check the version? -= Ed

Mar 31, 2022

E
Ed

When I ran commandbox as administrator it all worked perfectly :-) I probably should have thought of that myself but I've had one of those days. I did see one error but it doesn't look like it caused any problems. Mentioning here more for your reference: <code>Running the post install script... The script [postInstallAll] does not exist in this package.</code> The To Do app is now running nicely and I can add and remove entries from the list. It's a nice little demo app and I look forward to playing around with htx, it could solve a bunch of problems for me where I need ajax functions but can't (or don't have time to) build it with a framework. Let me also take this opportunity to say thanks for stepping up to the plate and taking over leadership of the project :-D

Mar 31, 2022

Latest Blog Posts