New Stimulus Design Pattern?
As far as I know, this is a new design pattern for Stimulus. At the very least, it isn’t something I was able to readily find with Google searches.
First, let’s gets some standard stuff out of the way. The Ruby2JS.com is built using the Bridgetown static site generator. Opal is used to generate the bulk of the scripts to be executed. HTTP caching ensures that these scripts are only downloaded when they change. Turbo ensures that these scripts are only loaded once per site visit. Stimulus associates HTML elements with controllers.
All standard stuff so far.
The source to the controllers can be found in GitHub. Unsurprisingly given that these controllers support the Ruby2JS site, they are written in Ruby.
But that’s not the unique design pattern part.
Look at a Ruby editor on any of the pages mentioned. There isn’t really any Actions, Targets, Values, or CSS Classes to speak of.
All of this is accomplished by subclassing a common base
source method with calls to a
findController method. The
findController method unsurprisingly searches the
array. This base class also takes care of connecting sources with targets
indpendent of the order in which the controllers connect.
As an example,
are the lines of code where
The JSController’s implementation of the
will dispatch the content to the jsEditor.
The EvalController’s implementation of the
will load the script into a
script element and append it to the document.
The way this works is that the Ruby souce code is initially converted to
option, and the results
are sent to the evaluation controller. The evaluation controller captures the
syntax error and given that this occurred on the very first update it will
options in the Ruby Controller, triggering another conversion, the
results of which are sent to both the JS and Eval controllers.
While this usage is quite different than the traditional application of
Stimulus, the end result is comparable: a site consisting entirely of static
HTML augmented with a small number of
data- attributes that cause the
controllers to activate.
I’m quite curious if others have seen this usage of Stimulus before, if they find it useful, or have any suggestions.