2011-06-22 11 views
0

GrailsのGSPからクラスのフィールドにアクセスするにはどうしたらいいですか?たとえば、次のようになります。GrailsのGSPからクラスフィールドにアクセスする

geocoder.geocode({ 
     'address': 'london' 
    }, 

ユーザーが挿入した後にプログラムでアドレスを取得する必要があります。次のような何か:

geocoder.geocode({ 
     'address': ${search.city} 
    }, 

検索はクラスであり、市がフィールドです。 これを行う方法はありますか?鑑み

def map = { 
    def searchInstance = Search.get(1) 
    [locationList : Location.list(), search:searchInstance] 
} 

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は、Fにアクセスすることができる

コントローラで : おかげ

UPDATE

私はこの試み地図のクロージャによって返されたロケーションリストのビューを表示するが、検索インスタンスにアクセスすることはできない。 アイデア

+0

'geocoder.geocode({...})'部分はすでにGSPの '$ {} '内にありますか? – Pat

+0

いいえ、javascriptスクリプトにありますが、$ {}内にはありません –

答えて

1

コントローラからビューへのモデルとしてそのクラスのインスタンスを渡すと、通常どおりフィールドにアクセスできます。

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} 
    }, 
} 
+0

あなたのソリューションは私の必要としているもののように見えますが、私のためには機能しません。おそらく私はsearch.gspではなくmap.gspで都市のフィールドが必要なので、おそらく。私はmap.gspを呼び出すために使用するマップと呼ばれるコントローラの追加のクロージャを持っています。それは例外を与えています:ヌルオブジェクトでプロパティ 'city'を取得できません –

+0

それはすべてのエラーafterallを与えません。しかし、この見解は決して現れません。コードを確認するには、質問の更新を参照してください。 –

+0

エラーが見つかりました。私は検索インスタンスを引用符で囲む必要がありました。これは次のようなものです。 'address': "$ {search.city}" –

関連する問題