1

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!");
                        }
                    )

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

This won't compile: not found: value successFunc.
What version of Play are you using? Are you sure you copied everything as in the showcase?
Make sure that successFunc and failureFunc declarations aren't placed in other code block.
Thanks that was the issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.