javascriptscalatemplatesplayframeworkscala-template

Play framework JavaScript function as scala template parameter


I want to reuse a javascript function using a scala template so I would only have to pass a different success/failure function, but I don't seem to be able to able to pass a javascript function to a scala template. Please note I'm veeeeerry new to this and don't even know if what I am doing is possible.

This is kind of what I'm trying to achieve:

@(formId: String, success: JavaScript, fail: JavaScript)
<script type="text/javascript">
    $("@formId").submit(function(e)
    {
        var data = $(this).serializeArray();
        var action = $(this).attr("action");
        $.ajax(
        {
            url : action,
            type: "POST",
            data : data,
            success:function(data, textStatus, jqXHR)           // Change contents to dynamic parameter for scala??? perhaps a javascript function to execute???
            {
                @success()
                /*console.log("save succesfull, progress!")
                alert('Save successfull, now move on!');*/
            },
            error: function(jqXHR, textStatus, errorThrown)     // Change contents to dynamic parameter for scala??? perhaps a javascript function to execute???
            {
                //if fails
                @fail()
                /*console.log(jqXHR.responseText);
                var errors = JSON.parse(jqXHR.responseText);
                console.log(errors);
                alert('Woops, something went wrong: ' + jqXHR.responseText);*/
            }
        });
        e.preventDefault();
    });
</script>

How it would be used:

@snippets.ajaxFormSubmit("#form",
                        function()
                        {
                            alert("Save successfull, now move on!");
                        },
                        function()
                        {
                            alert("Save failed!");
                        }
                    )

Solution

  • You can pass any content to a template via Html type.

    @(formId: String, success: Html, fail: Html)
    <script type="text/javascript">
        $("@formId").submit(function(e)
        {
            var data = $(this).serializeArray();
            var action = $(this).attr("action");
            $.ajax(
            {
                url : action,
                type: "POST",
                data : data,
                success:function(data, textStatus, jqXHR)           // Change contents to dynamic parameter for scala??? perhaps a javascript function to execute???
                {
                    @success
                },
                error: function(jqXHR, textStatus, errorThrown)     // Change contents to dynamic parameter for scala??? perhaps a javascript function to execute???
                {
                    @fail
                }
            });
            e.preventDefault();
        });
    </script>
    

    In a client view you can user it as follows:

    @successFunc = {
        alert("Save successfull, now move on!");
    }
    
    @failureFunc = {
        alert("Save failed!");
    }
    
    @snippets.ajaxFormSubmit("#form", successFunc, failureFunc)