2012-06-14 4 views
0

가 나는 장치의 위치를 ​​결정하기 위해 노력하고 응답 모델을 설정하고가 설정할 수 없습니다 모델 - 백본

class FoursquareSearch.Views.Origin extends Backbone.View 

events: 
    'change [name=origin]': 'setOrigin' 
    'click [name=geolocate]' : 'geolocate' 

    geolocate: -> 
    navigator.geolocation.getCurrentPosition(@handle) 

    handle: (response) -> 
    @model.set(coords: response) 

뷰가 있습니다. 그러나 나는 얻는다

Uncaught TypeError: Cannot call method 'set' of undefined 

이상한 물건은 이것의 내부의이 기능 만이 일어난다. 작동 같은 내부보기

geocode: (location) -> 
    data = 
     location: location 

    $.ajax(
     type: 'POST' 
     url: '/search/geocode' 
     data: data 
     dataType: 'json' 

     error: (jqXHR, textStatus, errorThrown) => 
     alert("ERROR") 


     success: (response, text, xhr) => 
     @model.set(coords: response) 
     @center(@model.get('coords')) 
     ) 

을, 그리고 그것을 잘 작동합니다 ... 난 그냥 모델을 설정하는 다른 기능을 얻을 수 그러나 예를 들어 내가 사용하는 경우. 나는 이것이 비동기적인 것에 관한 것이라고 생각한다. 나는 결코 이것에 전문가가 아니다, 내가가는 것에 따라 나는 백본을 집어 들고있다. 그러나 이것은 나를 비틀 거리고있다!

답변

2

Geolocation API 그래서 콜백 내부 this 아마 windowgetCurrentPosition 콜백 함수에 대한 특정 컨텍스트를 지정하지 않습니다; 존재하지 않는 window.modelset를 호출하는

handle: (response) -> 
    window.model.set(coords: response) 

그래서 handle 시도를하고있다 :

handle: (response) -> 
    @model.set(coords: response) 

getCurrentPosition 그것을 호출 할 때 다음과 같이 찾고 끝 : window 보통이 때문에 model 속성이되지 않습니다 Cannot call method 'set' of undefined 오류.

bound methodhandle를 정의하십시오 : @이보기 객체이고 그는 model 속성을 가지고 있기 때문에

handle: (response) => # fat arrow here 
    @model.set(coords: response) 

귀하의 다른 @model.set 전화가 잘 작동하고 있습니다.

+0

정말 고맙습니다. –