控制器在行动中丢失

时间:2013-05-15 21:57:42

标签: ember.js

我有一个在rails应用程序之上运行的ember.js应用程序。这个ember应用程序有一个地图和一个处理标记的视图。

每当我更改路线时,我必须销毁此视图中的标记。问题是:当我第一次输入generateMarker时,我得到了我的控制器(this.get("controller")),稍后我的dropend观察者我可以使用另一个this.get("controller") < / p>

但是当我更改路线并返回时,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'

1 个答案:

答案 0 :(得分: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 = @

现在工作正常