Michal_Szulc
Michal_Szulc

Reputation: 4177

Rendering few models in gson

Is it possible to send few objects into gson rendering view? I tried to use in controller:

respond trainings, [status: OK, view:"trainingsByClients", model: [myVariable: "test", anotherVariable: 123]]

and in gson view:

model {
    Iterable<Training> trainingList
    String myVariable
}

json {
    myVariable myVariable
    trainings tmpl.training(trainingList ?: [])
}

and it responds with:

{
  "myVariable": null,
  "trainings": [
    {
      "id": 3,
      "name": "test t",
      "numberOfAbsentClients": 0,
      "startDate": "2016-11-20T09:00:00+0000",
      "numberOfClients": 2,
      "section": {
    "id": 1,
    "name": "test sec"
      }
    },
    {
      "id": 10,
      "name": "test 2",
      "numberOfAbsentClients": 0,
      "startDate": "2016-11-09T11:00:00+0000",
      "numberOfClients": 2,
      "section": {
    "id": 2,
    "name": "sec 2"
      }
    }
  ]
}

Upvotes: 0

Views: 191

Answers (2)

daptordarattler
daptordarattler

Reputation: 96

Actually you have to use the g.render method for all model properties when using respond

example gson view:

  model {
    Iterable<Training> trainingList
    String myVariable
}

json {
    myVariable g.render(myVariable)
    trainings tmpl.training(trainingList ?: [])
}

This is only related to the models you parse in the respond.model parameter

Upvotes: 0

Michal_Szulc
Michal_Szulc

Reputation: 4177

ok, I found solution:

    render(view: "trainingsByClients", model: [trainingList: trainings, myVariable: "asdg"])

so we should use render instead of respond. Respond is not adding properly additional model objects.

Upvotes: 1

Related Questions