Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EmberJs: binding input element from Ember.TextField in the init function (Autocomplete google map)

Tags:

ember.js

I try to use the GoogleMap Autocomplete component. This Google Javascript service needs simply to be binded to an input field.

My idea was to create a new View extending TextField and do the binding as described bellow: I have a doubt regarding the "this" in the Autocomplete constructor (this, option). As you can easily understand,this is for me the input component... (the error it perhaps here)

App.AutoCompleAddressView = Ember.TextField.extend({
  type: 'text',
  init: function() {
    this._super();
    var options = {
      types: ['(cities)'],
      componentRestrictions: {country: 'fr'}
    };
  autocomplete = new google.maps.places.Autocomplete(this, options);
  }
});

and the html code template:

{{view App.AutoCompleAddressView}}

Unfortunately I get an Ember error: Uncaught TypeError: Object has no method 'getAttribute'

like image 741
fvisticot Avatar asked Sep 08 '26 12:09

fvisticot


1 Answers

if the google.maps.places.Autocomplete requires a DOM element, then you have to pass this.$() to it. I think you have do to that in the didInsertElement hook. Something like:

App.AutoCompleAddressView = Ember.TextField.extend({
  type: 'text',
  didInsertElement: function() {
    var options = {
      types: ['(cities)'],
      componentRestrictions: {country: 'fr'}
    };
 //this.$() return the element as an array form, but google autocomplete seems to not accept this. So take the first one.
    new google.maps.places.Autocomplete(this.$()[0], options);
  }
});

Added a minimal example: http://jsfiddle.net/6p6XJ/211/

like image 131
sly7_7 Avatar answered Sep 11 '26 12:09

sly7_7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!