0

非同期JSコード内にオブジェクトを作成しています。オブジェクトが作成されたら、そのオブジェクトのメソッドを呼び出したいと思います。新しいオブジェクトを参照してそのオブジェクトのメソッドを呼び出す方法に少し戸惑っています。

これが私のコーヒースクリプトです。コードはグーグルマップにマーカーを追加します:

addLocation: (name, id, lat, lng, options, callback) ->
  # Add a new location to the map
  # 
  # @param string name - name to give the location
  # @param int id - ID of the location if stored in the database already
  # @param double lat - latitude
  # @param double lng - longitude
  # @param json options - options to use while adding the location
  console.log("Adding new location '#{name}' (#{lat}, #{lng}) to map") if debug

  @__setVisible true, =>
    location = new Location(this, id, name)
    location.setLatLng(lat, lng, options)
    @locations.add(location)

    callback() if callback

    return location

そして、ここで私はこのメソッドを呼び出します。返された「location」オブジェクトのメソッドを呼び出したいのですが、まだインスタンス化されていないオブジェクトにコールバックでバインドするにはどうすればよいですか?

__addLocation: (resultLocation) ->
  # Add a new location to the map and centre the view in on it
  name = $(@nameElement).val() 
  lat = resultLocation.geometry.location.lat()
  lng = resultLocation.geometry.location.lng()

  @map.addLocation name, null, lat, lng, { draggable: true }, ->
    # location doesn't exist at this point so the following line fails
    location.setShowInfoWindowOnClick(true)
    @map.centerMap(location)

location.setShowInfoWindowOnClick(true)を実行するにはどうすればよいですか?

4

1 に答える 1

0

ではaddLocation、次のようにコールバックを呼び出します。

callback location

そして、あなたのコードで;

@map.addLocation name, null, lat, lng, { draggable: true }, (location) ->
    location.setShowInfoWindowOnClick true
    @map.centerMap location
于 2012-09-01T07:48:46.673 に答える