John
John

Reputation: 17

Grails - how to bind data to a map in a command object?

I have a table where each row has a toggle switch to pause/resume a scheduled task (I am using Quartz scheduler to do this). The screen where this table is displayed refreshes every 10 seconds. What is currently happening is that the selected toggle for the button is not being 'remembered' on each refresh.

So for example if I click the pause task to 'on' using the toggle switch, when the screen refreshes the toggle switch is set back to 'off' as the toggle selection is not being remembered.

My current implementation has a function that is fired when the toggle selection changes. This takes three parameters - name/group (for quartz functionality) and value (the id of the toggle switch in the table row that is used to get the checked value). Basically in this function I am mapping the id to the boolean checked value of the toggle switch:

function pause(name, group, value)
{
    var checked = $('#pauseScheduledTask' + value).prop('checked');
    var map = {}
    map[value] = checked
    var data = {
        taskName: name,
        taskGroup: group,
        idToCheckedValue: map,
    };

    var url = '<g:createLink controller="scheduler" action="pauseScheduledTask"/>';
    ajaxUpdateAction(url, data, null, null, null, null);
}

Controller:

    def pauseScheduledTask(PauseTaskCmd cmd)
    {
        // Logic to do here...
    }

My controller method takes this command object:

class PauseTaskCmd implements Validateable {

String taskName
String taskGroup
Map<String, Boolean> idToCheckedValue = new HashMap<>();

static constraints = {
    taskName nullable: false
    taskGroup nullable: false
 }

}

The String values in the command object are binded ok but the map is always empty in my controller. Is binding to maps a special case? How would I bind the map from my function to the map in the command object? If I can get the mappings in my command object I should then be able to use them to display the values when the screen refreshes.

Row toggle switch in table

Upvotes: 0

Views: 338

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27200

See the project at https://github.com/jeffbrown/johncommandobjectmap.

https://github.com/jeffbrown/johncommandobjectmap/blob/775914e60768993f79f151cfe12d99f4944dd0a3/src/main/groovy/demo/PauseTaskCmd.groovy

package demo

import grails.validation.Validateable

class PauseTaskCmd implements Validateable {

    String taskName
    String taskGroup
    Map<String, Boolean> idToCheckedValue = new HashMap<>();

    static constraints = {
        taskName nullable: false
        taskGroup nullable: false
    }
}

https://github.com/jeffbrown/johncommandobjectmap/blob/775914e60768993f79f151cfe12d99f4944dd0a3/grails-app/controllers/johncommandobjectmap/DemoController.groovy

package johncommandobjectmap

import demo.PauseTaskCmd

class DemoController {

    def pauseScheduledTask(PauseTaskCmd cmd) {
        render "cmd.idToCheckedValue -> ${cmd.idToCheckedValue}"
    }
}

If you were to post json structured like this, that should work:

{"idToCheckedValue":{"first key":"first value", "second key":"second value"}}
$ curl -H "Content-Type: application/json" -X POST -d '{"idToCheckedValue":{"first key":"first value", "second key":"second value"}}' http://localhost:8080/demo/pauseScheduledTask

cmd.idToCheckedValue -> [first key:first value, second key:second value]

Upvotes: 1

Related Questions