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'
怎麼做你的路線是什麼樣子? – intuitivepixel
加入我的路線,檢查我的編輯 –
一件事情:)你可以實際顯示控制器或者它的模型得到的設置?我的意思是備份'ChamadaMarkerView' – intuitivepixel