New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The Values and Classes APIs #202
Conversation
To be useful, date values need to be wrapped in a time zone-aware object. This reverts commit 2da572d.
This comment has been minimized.
This comment has been minimized.
Wait a minute…why is CRUNCH in all caps. Could it be… a new code name?? 😆 Just teasing. Good luck on the impending launch! |
It looks like this API is all but confirmed, if HEY source code is of any indication. P.S. Gratz on the launch. |
@panda-madness This API was confirmed when the guy who makes the library made the PR. 🤷 If you felt there was any ambiguity on the subject, were you not convinced by repeated comments from Basecamp folks that this is coming? I'm just very much hoping that there's still time and opportunity for default values to make it in before v2 officially launches. |
@leastbad there is no need to be toxic about it. Scrolling through this thread I cannot see a solid confirmation from any of the Basecamp guys, I asked a direct question some time ago that was left unanswered. Hence my perceived ambiguity about this PR's future. |
@sstephenson In the current alpha build the lifecycle method invokation order seems to be |
Is there any timeline on when this might get released? |
https://twitter.com/dhh/status/1291114354482524160 |
target attributes are now scope by the controller name see hotwired/stimulus#202
This pull request introduces two new APIs to Stimulus: Values and Classes. These APIs are designed to improve upon, and ultimately obviate, the current Data Map API. We plan to ship them together in the upcoming Stimulus 2.0 release.
Values
Most uses of the Data Map API in Basecamp fall under the following categories:
However, the Data Map API only works with string values. That means we must manually convert to and from other types as needed. The Values API handles this type conversion work automatically.
Value properties
The Values API adds support for a static
values
object on controllers. The keys of this object are Data Map keys, and the values declare their data type:Supported types and defaults
This pull request implements support for five built-in types:
JSON.stringify(array)
[]
boolean.toString()
false
number.toString()
0
JSON.stringify(object)
{}
""
Each type has a default value. If a value is declared in a controller but its associated data attribute is missing, the getter property will return its type's default.
Controller properties
Stimulus automatically generates three properties for each entry in the object:
this.[name]Value
data-[identifier]-[name]-value
this.[name]Values
data-[identifier]-[name]-values
this.[name]Value=
data-[identifier]-[name]-value
this.[name]Values=
data-[identifier]-[name]-values
this.has[Name]Value
data-[identifier]-[name]-value
this.has[Name]Values
data-[identifier]-[name]-values
Note that array values are always pluralized, both as properties and as attributes.
Value changed callbacks
In addition to value properties, the Values API introduces value changed callbacks. A value changed callback is a specially named method called by Stimulus whenever a value's data attribute is modified.
To observe changes to a value, define a method named
[name]ValueChanged()
. For example, a slideshow controller with a numericindex
property might define anindexValueChanged()
method to display the specified slide:Stimulus invokes each value changed callback once when the controller is initialized, and again any time the value's data attribute changes.
Even if a value's data attribute is missing when the controller is initialized, Stimulus will still invoke its value changed callback. Use the existential property to determine whether the data attribute is present.
Classes
Another common use of the Data Map API is to store CSS class names.
For example, Basecamp's copy-to-clipboard controller applies a CSS class to its element after a successful copy. To avoid inlining a long BEM string in our controller, and to keep things loosely coupled, we declare the class in a
data-clipboard-success-class
attribute:and access it using
this.data.get("successClass")
in the controller:The Classes API formalizes and refines this pattern.
Class properties
The Classes API adds a static
classes
array on controllers. As with targets, Stimulus automatically adds properties for each class listed in the array:this.[name]Class
data-[identifier]-[name]-class
attributethis.has[Name]Class
data-[identifier]-[name]-class
attribute is presentDeclarations are assumed to be present
When you access a class property in a controller, such as
this.supportedClass
, you assert that the corresponding data attribute is present on the controller element. If the declaration is missing, Stimulus throws a descriptive error:If a class is optional, you must first use the existential property (e.g.
this.hasSupportedClass
) to determine whether its declaration is present.Unifying target attributes
We've made a change to the target attribute syntax to align them with values and classes, and also to make the controller identifier more prominent by moving it into the attribute name.
The original syntax is:
and the updated syntax is:
The original syntax is supported but deprecated
Stimulus 2.0 will support both syntaxes, but using the original syntax will display a deprecation message in the developer console. We intend to remove the original syntax in Stimulus 3.0.
Try it out in your application
Update the Stimulus entry in
package.json
to point to the latest development build: