Require.js data-main attribute support

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Require.js data-main attribute support

JasonGardner
Hi I am attempting to write a attribute processor to support the Require.js data-main attribute.

I am using the following spring configuration

@Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources-" + env.getProperty("version") + "/**").addResourceLocations("/resources/**");
    }

Such that each version of the application requests resources from a versioned resource url.


my script tag markup is as follows

<script type="text/javascript" data-main="../../resources/js/main.js" src="../../resources/external/require/2.1.6/minified/require.js" require:data-main="@{'/resources-' + ${#messages.msg('version')} + '/js/main.js'}" th:src="@{'/resources-' + ${#messages.msg('version')} + '/external/require/2.1.6/minified/require.js'}"></script>

I have created a DataMainAttrProcessor extending AbstractStandardAttributeModifierAttrprocessor and associated this with the require dialect

however when I run the application I get a
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence:


Obviously AbstractStandardAttributeModifierAttrprocessor is not the appropriate class to extend can anybody point me in the right direction with regards to a class that will support URL resolution similar to that provide by th:src


Thankyou
Reply | Threaded
Open this post in threaded view
|

Re: Require.js data-main attribute support

Emanuel
Administrator
For creating an attribute processor, I think it's best to extend the AbstractAttrProcessor class, then you can do whatever you want in the processAttribute() method, which in your case would be to use the StandardExpressionProcessor.processExpression() method to turn the expression passed to your attribute processor into a URL.
Reply | Threaded
Open this post in threaded view
|

Re: Require.js data-main attribute support

JasonGardner
Thank you for the response

I have been able to implement my requirements with the following code



import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor;

public class DataMainAttrProcessor extends AbstractStandardSingleAttributeModifierAttrProcessor {

    public static final int ATTR_PRECEDENCE = 1700;
    public static final String ATTR_NAME = "data-main";


    public DataMainAttrProcessor() {
        super(ATTR_NAME);
    }


    @Override
    public int getPrecedence() {
        return ATTR_PRECEDENCE;
    }

    @Override
    protected String getTargetAttributeName(Arguments arguments, Element element, String attributeName) {
        return ATTR_NAME;
    }

    @Override
    protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
        return ModificationType.SUBSTITUTION;
    }

    @Override
    protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
        return true;
    }
}