2013-05-15 109 views
0

我有一個ember.js應用程序運行在rails應用程序之上。這個應用程序應用程序有一個地圖和一個視圖來處理標記。控制器在動作中丟失

每當我改變路線,我必須銷燬這個視圖內的標記。問題是:當我進入generateMarker首次得到我的控制器(this.get("controller")),後來在我的dropend觀察者,我可以用另一個this.get("controller")

得到,但是當我改變路線,回來generateMarker能得到控制器,但updateLocation不能。爲什麼?

首先運行:

generateMarker: -> 
    $controller = @get("controller") 
    # $controller gets the object 

然後我invoque:

updateLocation: (lat, lon, animate = true) -> 
     ... 
     @get("controller").center_passageiro() 
     # This also works. 

然後我移動到另一條路線回來,這一次:

generateMarker: -> 
    $controller = @get("controller") 
    # $controller gets the object and read valid information 

但後來:

updateLocation: (lat, lon, animate = true) -> 
     ... 
     @get("controller").center_passageiro() 
     # This gives me this error: Cannot call method 'center_passageiro' of null 
     # And inspecting it i see that this.get('controller') is now returning null 

我在做什麼錯?

這裏是整個類(使用的CoffeeScript)

Webapp.ChamadaMarkerView = Ember.View.extend 

    generateMarker: -> 
    $controller = @get("controller") 
    unless Webapp.chamadaMarker? 
     Webapp.chamadaMarker = @ 

    if $controller.get("mlat") isnt null && $controller.get("mlon") isnt null 
     lat = $controller.get("mlat") 
     lon = $controller.get("mlon") 
    else 
     lat = $controller.get("lat") 
     lon = $controller.get("lon") 

    @set("marker", new google.maps.Marker 
     position: new google.maps.LatLng(lat, lon) 
     map: window.map 
     draggable: ($controller.get("nodrag") isnt null) 
     animation: google.maps.Animation.Drop 
     icon:"/assets/icone_passageiro.png" 
    ) 

    google.maps.event.addDomListener(@get("marker") 
    , "dragend" 
    , -> 
     pos = Webapp.chamadaMarker.get("marker").getPosition() 
     $controller.updatePosMarker(pos.lat(), pos.lng()) 
     Webapp.chamadaMarker.updateLocation(pos.lat(), pos.lng()) 
    ) 

    updateLocation: (lat, lon, animate = true) -> 
    mk = @get("marker") 
    mk.setPosition(new google.maps.LatLng(lat, lon)) 
    if animate 
     mk.setAnimation(google.maps.Animation.DROP) 
     @get("controller").center_passageiro() 

    didInsertElement: -> 
    @generateMarker() 

    willDestroyElement: -> 
    @get("marker").setMap(null) 

編輯

我的路線

Webapp.Router.map -> 
    @route 'MapEmEspera', path: '/' 
    @route 'MapEmChamada', path: '/mapa' 
    @route 'cadastro', path: '/dados' 
    @route 'novo_cadastro', path: '/cadastrar' 
    @route 'chamada', path: '/chamar' 
    @route 'espera', path: '/aguardando' 
    @route 'taxista', path: '/taxista' 
+0

怎麼做你的路線是什麼樣子? – intuitivepixel

+0

加入我的路線,檢查我的編輯 –

+1

一件事情:)你可以實際顯示控制器或者它的模型得到的設置?我的意思是備份'ChamadaMarkerView' – intuitivepixel

回答

0

找到我的問題

generateMarker: -> # A generate method because I should create a global object 
    $controller = @get("controller") 
    unless Webapp.chamadaMarker? # if this object is already created ignore it 
    Webapp.chamadaMarker = @ 

但每到這個對象被銷燬時這給了我一個問題,ember.js幾乎不會設定一個對象null,每次我改變控制器I得到一個新的視圖時這樣,但全球對象不更新。

修復它,我只是添加此行:

geraMarker: -> 
    $controller = @get("controller") 
    if Webapp.chamadaMarker? 
    Webapp.chamadaMarker = null 
    Webapp.chamadaMarker = @ 

,現在它工作正常