Created
October 2, 2017 17:24
-
-
Save brendanoh/6b3eb6a7a40e8a574dd7be67a45fc918 to your computer and use it in GitHub Desktop.
The files below show the use of a `locatable-model` Mixin, along with the `ember-cli-g-maps` AddOn to allow any and all objects in an Ember App to be locatable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from 'ember-data/model'; | |
import attr from 'ember-data/attr'; | |
import locatableModel from 'foundingphilly/mixins/locatable-model'; | |
export default Model.extend(locatableModel, { | |
name: attr('string'), | |
domain: attr('string') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default Ember.Controller.extend({ | |
actions: { | |
setLocation(location) { | |
const user = this.get('user'); | |
user.setLocation(location); | |
this.saveUser(user); | |
}, | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
export default Ember.Mixin.create({ | |
address: DS.attr('string'), | |
addressUrl: DS.attr('string'), | |
city: DS.attr('string'), | |
lat: DS.attr('number'), | |
lng: DS.attr('number'), | |
displayName: Ember.computed('address', function() { | |
if (this.get('address.length')) { | |
const breakAt = this.get('address').indexOf(','); | |
const breakFrom = breakAt + 1; | |
const lineOne = this.get('address').substring(0, breakAt); | |
const lineTwo = this.get('address').substring(breakFrom); | |
return Ember.String.htmlSafe(lineOne.trim() + '<br>' + lineTwo.trim()); | |
} | |
}), | |
setLocation(location) { | |
const id = this.get('id'); | |
this.set('lat', location.lat); | |
this.set('lng', location.lng); | |
this.set('address', location.place.formatted_address); | |
this.set('addressUrl', location.place.url); | |
const addressComponents = location.place.address_components; | |
let city = ''; | |
addressComponents.forEach(function(ac) { | |
const political = ac.types.indexOf('political') !== -1; | |
const neighborhood = ac.types.indexOf('neighborhood') !== -1; | |
const locality = ac.types.indexOf('locality') !== -1; | |
const sublocality = ac.types.indexOf('sublocality') !== -1; | |
const administrative = ac.types.indexOf('administrative_area_level_1') !== -1; | |
if (!city && political && (neighborhood || locality || administrative || sublocality)) { | |
city = ac.long_name; | |
} | |
}); | |
this.set('city', city); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment