Stimulus: Feature Request: Nicer syntax for action descriptors

Created on 2 Mar 2018  路  4Comments  路  Source: hotwired/stimulus

Hi guys! First of all, I'd like to know if this is on the roadmap or if it's up for debate.
If the latter, I'd like to suggest the syntax: click::hello.greet or click::hello@greet.
IMHO current syntax is a bit rough on the eyes (not to say ugly) 馃榿.

Most helpful comment

No plans to change the syntax, beyond exploring places where you can use action and target naming without explicitly referring to the controlled scope, when that can be done unambiguously.

All 4 comments

I think current syntax is better...

However, if you use slim, I have a monkey patch to clean the "data-*" attributes so the template will look like this:

/ hello.slim
div @controller="hello"
  input type="text" @target="hello.name"
  button @click="hello#greet" Greet
  span @target="hello.output"

The following patch to slim parser just matches HTML attributes starting with @ and do transforms:

# test.rb
require 'slim'

module Slim
  class Parser
    def parse_attributes(attributes)
      # Check to see if there is a delimiter right after the tag name
      delimiter = nil
      if @line =~ @attr_list_delims_re
        delimiter = @attr_list_delims[$1]
        @line = $'
      end

      if delimiter
        boolean_attr_re = /#{@attr_name}(?=(\s|#{Regexp.escape delimiter}|\Z))/
        end_re = /\A\s*#{Regexp.escape delimiter}/
      end

      while true
        case @line
        when @splat_attrs_regexp
          # Splat attribute
          @line = $'
          attributes << [:slim, :splat, parse_ruby_code(delimiter)]

        when /\A\s*@(\w+)\s*=\s*(["'])([\w\.\-\>\#]+)\2/
          attr_value = $3
          if attr_value['#']
            @line = $'
            attr_value = "#$1->#{attr_value}"
            attributes << [:html, :attr, 'data-action', [:slim, :interpolate, attr_value]]
          else
            case $1
            when 'controller'
              @line = $'
              attributes << [:html, :attr, 'data-controller', [:slim, :interpolate, attr_value]]
            when 'target'
              @line = $'
              attributes << [:html, :attr, 'data-target', [:slim, :interpolate, attr_value]]
            else
              syntax_error!('Must use "@controller=" or "@target=" or assign "controller#action"')
            end
          end

        when @quoted_attr_re
          # Value is quoted (static)
          @line = $'
          attributes << [:html, :attr, $1,
                         [:escape, $2.empty?, [:slim, :interpolate, parse_quoted_attribute($3)]]]
        when @code_attr_re
          # Value is ruby code
          @line = $'
          name = $1
          escape = $2.empty?
          value = parse_ruby_code(delimiter)
          syntax_error!('Invalid empty attribute') if value.empty?
          attributes << [:html, :attr, name, [:slim, :attrvalue, escape, value]]

        else
          break unless delimiter

          case @line
          when boolean_attr_re
            # Boolean attribute
            @line = $'
            attributes << [:html, :attr, $1, [:multi]]
          when end_re
            # Find ending delimiter
            @line = $'
            break
          else
            # Found something where an attribute should be
            @line.lstrip!
            syntax_error!('Expected attribute') unless @line.empty?

            # Attributes span multiple lines
            @stacks.last << [:newline]
            syntax_error!("Expected closing delimiter #{delimiter}") if @lines.empty?
            next_line
          end
        end
      end
    end
  end
end

puts Slim::Template.new('hello.slim').render binding

Then it generates:

<div data-controller="hello">
  <input data-target="hello.name" type="text" />
  <button data-action="click->hello#greet">Greet</button>
  <span data-target="hello.output"></span>
</div>

@luikore I'm not refering to the syntax for the data attributes per se. I'm talking about the "template" for defining event->controller#action. Maybe for people who use rails it's a little bit more natural (because of the route templates).

I think it's more about who likes what more. Changes will not affect functionality or a learning curve.
But will create more work to migrate to the next version.

No plans to change the syntax, beyond exploring places where you can use action and target naming without explicitly referring to the controlled scope, when that can be done unambiguously.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jenkijo picture jenkijo  路  3Comments

pherris picture pherris  路  3Comments

angarc picture angarc  路  3Comments

savroff picture savroff  路  4Comments

gerdriesselmann picture gerdriesselmann  路  3Comments