Advertisement
Guest User

Untitled

a guest
Feb 19th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. logPrefix = '[app.modules.Geo]: '
  2. Geo = Backbone.Model.extend
  3.   localStorage: new Backbone.LocalStorage('app:geo')
  4.  
  5.   expires: 1000 * 60 * 20 # 10 minutes
  6.  
  7.   defaults:
  8.     lat: null
  9.     lng: null
  10.  
  11.     place: null
  12.  
  13.     local_phone: null
  14.  
  15.     updated: 0
  16.  
  17.   _fetched: (model, attrs, opts)->
  18.     console.warn @attributes
  19.     if (app.now - @get('updated') > @expires)
  20.       app.log(logPrefix + 'data expired, updating')
  21.       @getLocation()
  22.  
  23.   _fetchLocationByCoords: ->
  24.     $.ajax(
  25.       url: app.api('geo_coords', [@get('lat'), @get('lng')])
  26.       dataType: 'json'
  27.       success: (resp)=>
  28.         @set('place', resp.value)
  29.         @set('updated', Date.now())
  30.  
  31.         @_fetchLocalPhone()
  32.         @save()
  33.       )
  34.  
  35.   _fetchLocationByIP: ->
  36.     $.ajax(
  37.       url: app.api('geo_ip', place)
  38.       dataType: 'json'
  39.       success: (resp)=>
  40.         @set('place', resp.value)
  41.         @set('updated', Date.now())
  42.  
  43.         @_fetchLocalPhone()
  44.         @save()
  45.       )
  46.  
  47.   _fetchLocalPhone: ->
  48.     $.ajax(
  49.       url: app.api('geo_phone', @get('place').country_name)
  50.       dataType: 'json'
  51.       success: (resp)=>
  52.         @set('local_phone', resp.value)
  53.         @save()
  54.       )
  55.  
  56.   initialize: ->
  57.     _fetched = _.bind(@_fetched, @)
  58.     @fetch(success: _fetched, error: _fetched)
  59.     app.log(logPrefix + 'initialize')
  60.  
  61.   getLocation: ->
  62.     geoSuccess = (position)=>
  63.       @set('lat', position.coords.latitude)
  64.       @set('lng', position.coords.longitude)
  65.  
  66.       @_fetchLocationByCoords()
  67.       app.log(logPrefix + 'located user')
  68.  
  69.     geoError = (error)=>
  70.       @_fetchLocationByIP()
  71.       app.log(logPrefix + 'location error code ' + error.code)
  72.  
  73.     if window.navigator.geolocation
  74.       window.navigator.geolocation.getCurrentPosition(geoSuccess, geoError)
  75.     else
  76.       @_fetchLocationByIP()
  77.  
  78. app.modules.Geo = Geo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement