Search code examples
ajaxstringtapestry

pass string to server side with ajax in tapestry


<t:if test="needsSetName">
        <label for="userfunction">${message:setname}</label>
        <t:textfield
            t:mixins="zoneUpdater"
            tabindex="-1"
            autofocus="false"
            ZoneUpdater.clientEvent="keyup"
            ZoneUpdater.event="valueChangedSetName"
            ZoneUpdater.zone="transferZone"
            ZoneUpdater.timeout="3000"
            t:id="setName"
            value="setName"/>
    </t:if>


@OnEvent(value = "valueChangedSetName")
protected void onValueChangedSetName(@RequestParameter(value = "param", allowBlank = true) String setName)
{
    TransferOrder to = baskets.getTransferOrder();
    this.setName = setName;
    to.setComment("Rename from " + this.setName + " to " + setName);
    nextSetName = setName;
    zoneHubService.updatePendingTransfer();

    zoneHubService.addCallback(new JavaScriptCallback()
    {
        @Override
        public void run(JavaScriptSupport javascriptSupport)
        {
            javascriptSupport.addScript(
                String.format("var thing=jQuery('#%s'); thing.focus();thing[0].setSelectionRange(10000, 10000);",
                    setNameField.getClientId()));
        }
    });
}


So my problem is, when i type some text into the textfield, it removes symbols like #,&,.. and everything else that comes after these symbols
When i use +, it turns into space.
The string that i get from the server in my method is already "edited".
I'm new to tapestry and ajax and i don't know how to solve this problem.
What do i have to do, so that i get the string back from the server without the server removing these symbols?


Solution

  • I solved my problem.
    In my zone-updater.js I had to use encodeURIComponent on my string.
    For anyone who has the same problem here is the link to the zoneUpdater.js code.
    http://tinybits.blogspot.com/2010/03/new-and-better-zoneupdater.html
    The link I, that I found to solve the bug is down.

    define([ "jquery", "t5/core/zone" ], function($, zoneManager) {

    return function(elementId, clientEvent, listenerURI, zoneElementId, timeout) {
    var $element = $("#" + elementId);
    var mytimeout;
    if (clientEvent) {
        $element.on(clientEvent, updateZone);
    }
    
    function updateZone() {
        jQuery($element).removeClass('saved');
        if (mytimeout != null) {
        clearTimeout(mytimeout);
        }
        mytimeout = setTimeout(function() {
        var listenerURIWithValue = listenerURI;
        if ($element.val()) {
            listenerURIWithValue = appendQueryStringParameter(
                listenerURIWithValue, 'param', $element.val());
            listenerURIWithValue = appendQueryStringParameter(
                listenerURIWithValue, 'element', elementId);
        }
        zoneManager.deferredZoneUpdate(zoneElementId,
            listenerURIWithValue);
        }, timeout);
    }
    }
    
    function appendQueryStringParameter(url, name, value) {
    if (url.indexOf('?') < 0) {
        url += '?'
    } else {
        url += '&';
    }
    value = encodeURIComponent(value);
    url += name + '=' + value;
    return url;
    }
    

    });