Stimulus JavaScript Framework Released
Written by Kay Ewbank   
Thursday, 22 February 2018

Stimulus, a new JavaScript Framework that revolves around just three main concepts: controllers, actions, and targets, was recently open-sourced by Basecamp and has now has reached Version 1.0.

Stimulus was originally envisioned by Basecamp CTO and Ruby on Rails creator, David Heinemeier Hansson. It is described as a JavaScript framework with modest ambitions. It doesn't seek to take over your entire front-end, and isn't concerned with rendering HTML at all. The aim is to augment your HTML with just enough behavior to make it shine.

Stimulus works by continuously monitoring the page, waiting for a specific attribute to appear. The data-controller attribute is used to connect and disconnect Stimulus controllers. The heart of Stimulus is its ability to automatically connect DOM elements to JavaScript objects. Those objects are called controllers in Stimulus.

The developers of Stimulus say that in the same way that class is a bridge connecting HTML to CSS, data-controller is a bridge from HTML to JavaScript. A second attribute, the data-action attribute, describes how events on the page should trigger controller methods, and a data-target attribute gives you a handle for finding elements in the controller’s scope.

stimulus

Stimulus builds on another Basecamp library called Turbolinks. This is a development that makes use of an approach called pjax that was developed at GitHub. The developers at Basecamp say that the reason full-page refreshes can feel slow is not because the browser has to process HTML sent from a server, nor the fact that an HTML payload tends to be larger than a JSON payload, because browsers handle this well. What causes the timelag is because the CSS and JavaScript have to be reinitialized and reapplied to the page again, even if the files themselves are cached.

To get around this reinitialization, Turbolinks maintains a persistent process, just like single-page applications do. The process is largely an invisible one, and it intercepts links and loads new pages via Ajax.

This approach is different from most contemporary JavaScript frameworks where JSON is turned into DOM elements via a template language. It also differs in the way state is handled. While most frameworks maintain state within JavaScript objects, and then render HTML based on that state, Stimulus stores state in the HTML, so that controllers can be discarded between page changes, but still reinitialize as they were when the cached HTML appears again.

The developers say this approach offers a valid alternative to the mainstream understanding of what a “modern” web application looks like, and can also be used in conjunction with other, heavier approaches. Stimulus is available under the MIT open source license and is available on GitHub.

stimuluslogo 

More Information

Stimulus Website

Related Articles

Next.js 5 Improves Webpack Support

jQuery Still Our Favourite Framework

React 16 Adds Fragments

The Performance Of AngularJS

React 16 Adds Fragments

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


GitHub Announces Open Source Security Fund
03/12/2024

A new security-focused program, the GitHub Secure Open Source Fund, will invest $1.25M across 125 open source projects. The project is backed by the support of organizations including American Express [ ... ]



OpenSilver Adds XAML Designer For Visual Studio Code
12/12/2024

OpenSilver 3.1 has been released. This version adds a drag-and-drop XAML designer for Visual Studio Code (VS Code), a new modern UI theme, and expanded support for WPF features. The open-source altern [ ... ]


More News

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 22 February 2018 )