0

我有一个在 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'
4

1 回答 1

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,这样每次我更改控制器时我都会得到一个新视图,但全局对象没有更新。

要修复它,我只需添加以下行:

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

现在它工作正常

于 2013-05-17T12:22:10.910 回答