Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- logPrefix = '[app.modules.Geo]: '
- Geo = Backbone.Model.extend
- localStorage: new Backbone.LocalStorage('app:geo')
- expires: 1000 * 60 * 20 # 10 minutes
- defaults:
- lat: null
- lng: null
- place: null
- local_phone: null
- updated: 0
- _fetched: (model, attrs, opts)->
- console.warn @attributes
- if (app.now - @get('updated') > @expires)
- app.log(logPrefix + 'data expired, updating')
- @getLocation()
- _fetchLocationByCoords: ->
- $.ajax(
- url: app.api('geo_coords', [@get('lat'), @get('lng')])
- dataType: 'json'
- success: (resp)=>
- @set('place', resp.value)
- @set('updated', Date.now())
- @_fetchLocalPhone()
- @save()
- )
- _fetchLocationByIP: ->
- $.ajax(
- url: app.api('geo_ip', place)
- dataType: 'json'
- success: (resp)=>
- @set('place', resp.value)
- @set('updated', Date.now())
- @_fetchLocalPhone()
- @save()
- )
- _fetchLocalPhone: ->
- $.ajax(
- url: app.api('geo_phone', @get('place').country_name)
- dataType: 'json'
- success: (resp)=>
- @set('local_phone', resp.value)
- @save()
- )
- initialize: ->
- _fetched = _.bind(@_fetched, @)
- @fetch(success: _fetched, error: _fetched)
- app.log(logPrefix + 'initialize')
- getLocation: ->
- geoSuccess = (position)=>
- @set('lat', position.coords.latitude)
- @set('lng', position.coords.longitude)
- @_fetchLocationByCoords()
- app.log(logPrefix + 'located user')
- geoError = (error)=>
- @_fetchLocationByIP()
- app.log(logPrefix + 'location error code ' + error.code)
- if window.navigator.geolocation
- window.navigator.geolocation.getCurrentPosition(geoSuccess, geoError)
- else
- @_fetchLocationByIP()
- app.modules.Geo = Geo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement