简体   繁体   English

将值从余烬组件发送到父模板

[英]send value from ember component to parent template

I want to pass lat and lng from an ember component to another ember component ( g-map ). 我想将latlng从余烬组件传递到另一个余烬组件( g-map )。 My handlebars template: 我的车把模板:

  {{!-- 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>

and this is for my text-autocomplete component: 这是我的文本自动完成组件:

// 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();
        });
    }
});

I want to pass the lat and lng values from text-autocomplete component to the g-map component to draw a marker in the google map. 我想将latlng值从text-autocomplete组件传递到g-map组件,以在Google地图中绘制标记。

Can anyone solve this? 谁能解决这个问题? :( :(

Create index.js controller file and introduce lat , lng and zoom properties. 创建index.js控制器文件,并介绍latlngzoom属性。 you can pass this properties to components text-autocomplete and g-map . 您可以将此属性传递给组件text-autocompleteg-map text-autocomplete this component should send actions to controller for updating new values for lat and lng because of two way binding it will automatically update in other places too. text-autocomplete此组件应将操作发送到控制器,以更新latlng新值,因为两种方式绑定也将在其他位置自动更新。

index.js controller file, 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 index.hbs

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

text-autocomplete.js file 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.
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM