在转换到新路由之前处理异步调用

时间:2013-10-15 07:12:10

标签: ember.js

我想获得用户Geolocation。一旦我得到坐标,我想根据Geolocation加载记录。如果结果超过1,请显示索引页

LHW.Store = DS.Store.extend
  revision: 11
  adapter: DS.RESTAdapter

DS.RESTAdapter.reopen
  namespace: 'api'

LHW.Company = DS.Model.extend
  name: DS.attr 'string'
  address: DS.attr 'string'
  city: DS.attr 'string'

LHW.IndexRoute = Ember.Route.extend
  setupController: (controller, route) ->
    # get Geolocation
    _route = @
    navigator.geolocation.getCurrentPosition ((location) ->
      #success
      _route.geoLocation(location)
     ), (error) ->
      #error
      switch(error.code)
        when error.PERMISSION_DENIED then console.log "User denied the request for Geolocation."
        when error.POSITION_UNAVAILABLE then console.log "Location information is unavailable."
        when error.TIMEOUT then console.log "The request to get user location timed out."
        when error.UNKNOWN_ERROR then console.log "An unknown error occurred."

  geoLocation: (location) ->
    _route = @
    @get('store').findQuery('company',{longitude: location.coords.longitude, latitude: location.coords.latitude}).then ((data) =>
      _route.controllerFor('companies').set('content', data)
      if data.get('length') > 1
        _route.transitionTo('companies');

    ), (error) ->
      console.log "error"

LHW.CompaniesRoute = Ember.Route.extend()

LHW.IndexController = Ember.ArrayController.extend
  beforeModel: ->
    console.log 'before'

所以这就是我现在所拥有的。但这有一些问题

  1. 如果你去/公司而不是/它跳过地理位置 校验。但是,如果我将地理位置移动到公司路线,那么 地理位置获取时用户看到的页面是什么 牵强?
  2. 创建交换机并检查路由中的长度, 感觉有点脏,因为Companyroute应该回应那个
  3. 所以这主要是一个流程问题,我无法解决这个问题,异步地理定位不会让事情变得更容易......

0 个答案:

没有答案