2012-11-04 4 views
1

백본, coffescript 및 Google지도 API로 작업하려고합니다. 지도를 렌더링하고 가운데 표식을 추가 할 수 있습니다. 지도에 마커가있는 위치 컬렉션을 추가하는 데 문제가 있습니다. 아래의 @map 객체를 응용 프로그램의 다른 부분이나 다른 부분과 공유하려면 어떻게해야합니까?백본/커피의 다른보기 기능에서 @ 맵 개체에 어떻게 액세스합니까?

addMarker에서 @map은 정의되지 않았습니다.

render: -> 
    $(@el).html(@template()) 
    primary = @collection.at(@collection.length - 1) 
    if primary 
     latlng = {} 
     @collection.each(@appendLocation) 
     latlng['latitude'] = primary.attributes.latitude; 
     latlng['longitude'] = primary.attributes.longitude; 
     @renderMap(latlng) 
    this 

    renderMap: (latlng) -> 
    view = new Bone.Views.Map() 
    $('#map').append(view.render().el) 
    latlng = new google.maps.LatLng(latlng['latitude'], latlng['longitude']) 
    myOptions = 
     zoom: 12 
     center: latlng 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    @map = new google.maps.Map(view.render().el, myOptions) 
    marker = new google.maps.Marker({ 
     position: latlng, 
     animation: google.maps.Animation.DROP, 
     map: @map, 
     title:"Hello World!" 
    }) 
    @collection.each(@addMarker) 

    addMarker: (location)-> 
    console.log(@map) <-----UNDEFINED 
    latlng = new google.maps.LatLng(location.attributes.latitude, location.attributes.longitude) 
    console.log location.attributes.latitude 
    location_marker = new google.maps.Marker({ 
     position: latlng, 
     animation: google.maps.Animation.DROP, 
     map: @map, 
     title:"Hello World!" 
    }) 

답변

0

나는 당신의 들여 쓰기 문제를 그냥 복사/붙여 넣기 오류이라고 가정합니다 및 코드 정말이 같다고 :

renderMap: (latlng) -> 
    view = new Bone.Views.Map() 
    $('#map').append(view.render().el) 
    latlng = new google.maps.LatLng(latlng['latitude'], latlng['longitude']) 
    myOptions = 
    zoom: 12 
    center: latlng 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    @map = new google.maps.Map(view.render().el, myOptions) 
    marker = new google.maps.Marker({ 
    position: latlng, 
    animation: google.maps.Animation.DROP, 
    map: @map, 
    title:"Hello World!" 
    }) 
    @collection.each(@addMarker) 

each method on a collection은 밑줄의 each과 단지 얇은 래퍼입니다 fine manual이 말을 가지고 each에 대한 :

,210 별칭 :는 반복자 함수 차례로 각각 산출 요소 리스트 위에 forEach

반복하여. 이터레이터컨텍스트 개체 (전달 된 경우)에 바인딩됩니다. 당신이 each를 사용할 때

당신은 컨텍스트를 지정하지 않을 :

@collection.each(@addMarker) 

그래서 @ (AKA this가) window 것 (또는 글로벌 컨텍스트 사용자 환경에서 무엇이든) addMarker 내부. bound function으로 addMarker

@collection.each(@addMarker, @) 

또는 정의 : 당신은 컨텍스트를 지정하거나 있도록 @이보기가되고 싶어요 당신은 또한 뷰의 initialize 방법 _.bindAll를 사용할 수

addMarker: (location) => 
    #... 

하지만 커피 스크립트와 드물다.

관련 문제