我有一个在 rails 应用程序之上运行的 ember.js 应用程序。这个 ember 应用程序有一个地图和一个视图来处理标记。
每次我改变路线时,我都必须破坏这个视图内的标记。问题是:当我generateMarker
第一次进入时,我得到了我的控制器(this.get("controller")
),后来在我的dropend
观察者上,我可以用另一个得到它this.get("controller")
但是当我改变路线并回来时,generateMarker
能够获得控制器但updateLocation
不能。为什么?
第一次运行:
generateMarker: ->
$controller = @get("controller")
# $controller gets the object
然后我调用:
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'