How can I access fields of classes from a GSP in Grails? For instance, the following:
geocoder.geocode({
'address': 'london'
},
I need to get the address programmatically after being inserted by the user. Something as the following:
geocoder.geocode({
'address': ${search.city}
},
where search is the class and city is the field. Is there a way to do this? Thanks
UPDATE
I tried this: In the controller:
def map = {
def searchInstance = Search.get(1)
[locationList : Location.list(), search:searchInstance]
}
In the view:
function initialize() {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': ${search.city}
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
var myMapOptions = {
zoom: 8,
center: results[0].geometry.location,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myMapOptions);
<g:each in="${locationList}" status="i" var="location">
var point${location.id} = new google.maps.LatLng(${location.lat}, ${location.lng});
var myMarkerOptions${location.id} = {
position: point${location.id},
map: map
};
if(map.getCenter().distanceFrom(point${location.id}) < 500000)
var marker${location.id} = new google.maps.Marker(myMarkerOptions${location.id});
</g:each>
}
});
}
I can access from the view the Location list returned by the map closure, but I can not access the search instance. Any ideas?
if you pass an instance of that class as a model from your controller to your view, you can access the fields just as you normally would.
Controller: searchController {
def search = {
def searchInstance = Search.get(1) //assuming this gets the search that you want
[search:searchInstance] // return searchInstance to the view under the alias search
}
}
Gsp: search.gsp {
geocoder.geocode({
'address': ${search.city}
},
}