Skip to content

Instantly share code, notes, and snippets.

@brendanoh
Created October 2, 2017 17:24
Show Gist options
  • Save brendanoh/6b3eb6a7a40e8a574dd7be67a45fc918 to your computer and use it in GitHub Desktop.
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.
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')
});
export default Ember.Controller.extend({
actions: {
setLocation(location) {
const user = this.get('user');
user.setLocation(location);
this.saveUser(user);
},
}
});
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);
}
});
{{g-autocomplete value=details.address options=myOptions on-select=(action "setLocation")}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment