将值从ember组件发送到父模板

时间:2017-03-14 03:23:13

标签: javascript google-maps ember.js ember-components ember-controllers

我想将latlng从ember组件传递到另一个ember组件(g-map)。我的车把模板:

  {{!-- Index.hbs --}}
  <div class="jumbotron-outside">
  <div class="jumbotron">
    <h1 class="display-3">See The Weather Outside :)</h1>
    <p class="lead">This is a simple forecast weather.</p>
    <hr class="my-4">
    <p>Just type everything bellow this input text to get all list of the city</p>
    {{text-autocomplete}}
    <p class="lead">
      <button class="btn btn-primary btn-default" href="#" role="button" disabled={{isDisabled}}>Search</button>
    </p>
  </div>
  {{g-map lat=lat lng=lng zoom=zoom}}
</div>

这是我的text-autocomplete组件:

// text-autocomplete/component.js
import Ember from 'ember';

let lat;
let lng;
export default Ember.Component.extend({
    didInsertElement() { //dom can be acessed here :)
        var autocomplete = new google.maps.places.Autocomplete($('input')[0]);
        var parent = this.$('input');
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            lat = place.geometry.location.lat();
            lng = place.geometry.location.lng();
        });
    }
});

我想将latlng组件中的text-autocompleteg-map值传递给library(drc) fit.ll <- drm(y~x, data=data.frame(x,y), fct=LL.5(), type="continuous") print(summary(fit.ll)) plot(fit.ll) 组件,以便在Google地图中绘制标记。

有人能解决这个问题吗? :(

1 个答案:

答案 0 :(得分:5)

创建index.js控制器文件并引入latlngzoom属性。您可以将此属性传递给组件text-autocompleteg-maptext-autocomplete此组件应向控制器发送操作以更新latlng的新值,因为双向绑定也会在其他位置自动更新。

index.js控制器文件,

import Ember from 'ember';
export default Ember.Controller.extend({
    lat:'',
    lng:'',
    zoom:'',
    actions:{
        updateLatAndLng(lat,lng){
            this.set('lat',lat);
            this.set('lng',lng);
        }
    }
});

index.hbs

{{text-autocomplete lat=lat lng=lng updateLatAndLng=(action 'updateLatAndLng')}}
{{g-map lat=lat lng=lng zoom=zoom}}

text-autocomplete.js文件

import Ember from 'ember';
export default Ember.Component.extend({
    didInsertElement() { //dom can be acessed here :)
        var autocomplete = new google.maps.places.Autocomplete($('input')[0]);
        var parent = this.$('input');
        let _this = this;
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            lat = place.geometry.location.lat();
            lng = place.geometry.location.lng();
            _this.sendAction('updateLatAndLng',lat,lng); //here we are sendin actions to controller to update lat and lng properties so that it will reflect in all the other places.
        });
    }
});