windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Polymer trigger function in child element using event from parent function

Asked By: Anonymous

I have an element with a child element that acts as a button. When I press ‘Enter’ in the parent element’s input box, I want to trigger the child’s _runNavigation method. What is the best way to create a custom trigger where the parent fires an event to the child element?

I have tried created an EventListener in my child element:

<...>
<button type="button" class="btn btn-success" on-click="_runNavigation">{{result}}</button
<...>

Polymer({

is: 'search-button',

properties: {
    searchQuery: {
        type: String,
    }
},

ready : function (){
    document.addEventListener('fire', this._runNavigation);
},

navigate: function(url) {
    var win = window.open(url, '_blank');
    if (win != null) {
        win.focus();
    }
},


_runNavigation: function() {
    var text = this.searchQuery;
    this.navigate("https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=" + text); 
},

});

And firing the event when enter is pressed when the text box is in focus:

<...>
  <input id="input" type="text" value="{{searchQuery::input}}" on-keydown="checkForEnter" on-change="_inputChanged" class="form-control" placeholder="Search">
<...>


checkForEnter: function(e) {
    // check if 'enter' was pressed
    if (e.keyCode === 13) {
    // enter pressed!
    console.log("ENTER!");
    this.fire('fire', {searchQuery: this.searchQuery}); 
    }
}

While this will fire an event that is picked up by the child element, ‘this.navigate’ will not run because ‘this’ is now the document. I have tried changing the event listener to
this.addEventListener(‘fire’, this._runNavigation);
to add it to the element itself, but then the element does not detect the trigger from the parent element.


Solution

Answered By: Anonymous

If you had no other choice but to use document.addEventListener from within your Polymer element, you’d have to set the context of this._runNavigation with bind():

ready: function() {
  document.addEventListener('fire', this._runNavigation.bind(this)); // avoid!
}

While this would work in your example, it listens to the fire event on the entire document, so if any other element outside the heirarchy of your form fired that event, it would trigger your element’s handler, which might be undesirable. For example:

  <x-form></x-form> <!-- listens to all 'fire' events -->

  <script>
    setTimeout(function() {
       // unrelated 'fire'
       document.dispatchEvent(new Event('fire'));
    }, 1000);
  </script>

codepen

As you might’ve guessed, Polymer provides the API to fire an event on a child element…

To dispatch an event to a child, you’d set a couple options when calling fire().

fire(type, [detail], [options]). Fires a custom event. The options object can contain the following properties:

  • node. Node to fire the event on (defaults to this).

  • bubbles. Whether the event should bubble. Defaults to true.

  • cancelable. Whether the event can be canceled with preventDefault. Defaults to false.

In your case, the bubbles and node options would be useful:

this.fire('fire', { searchQuery: this.searchQuery }, { bubbles:false, node: this.$.searchButton });

Then, in your search-button element, you’d use:

this.addEventListener('fire', this._runNavigation); // bind() not necessary here

Note in this demo that the fire event does not bubble up to the document (no warning logged in event handler).

codepen

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort http://www.kadinescort.net Gaziantep escort Denizli escort Adana escort Hatay escort Aydın escort İzmir escort Ankara escort Antalya escort Bursa escort İstanbul escort Kocaeli escort Konya escort Muğla escort Malatya escort Kayseri escort Mersin escort Samsun escort Sinop escort Tekirdağ escort Eskişehir escort Yalova escort Rize escort Amasya escort Balıkesir escort Çanakkale escort Bolu escort Erzincan escort Şırnak escort Van escort Yozgat escort Zonguldak escort Afyon escort Adıyaman escort Bilecik escort Aksaray escort Ağri escort Bitlis escort Siirt escort Çorum escort Burdur escort Diyarbakir escort Edirne escort Düzce escort Erzurum escort Kırklareli escort Giresun escort Kilis escort Kars escort Karabük escort Kırıkkale escort Mardin escort Kırşehir escort Maraş escort Manisa escort Muş escort Kastamonu escort Ordu escort Nevşehir escort Sakarya escort Osmaniye escort Şanliurfa escort Sivas escort Trabzon escort Tokat escort Ardahan escort Bartın escort Karaman escort Batman escort Bayburt escort Bingöl escort Elaziğ escort Gümüşhane escort Hakkari escort Isparta escort Uşak escort Igdır escort