0

Context: I have a use case where in the components used is firebase realtime database in Polymer 2.x. I have a function that does transformations on certain user activity with the firebase realtime object. I used polymerfire webcomponent.

What I want is: on user closes tab or window, save the data into firebase realtime database and then close the window.

Result: I used on beforeunload of window in the script of the polymer element. But not able to fire a method inside polymer element that triggers storage of the data in realtime db.

What I am looking for: 1. A way to initiate the element public function from script of an element. Or 2. a better way to implement on window/tab close save the data

Thanks.

Code:

<!DOCTYPE html>
<dom-module id="t-page">
<template is="dom-bind">
....
</template>
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' :
  'beforeunload'; /// make IE7, IE8 compitable

myEvent(chkevent, async function (e) { // For >=IE7, Chrome, Firefox
  var confirmationMessage = 'Are you sure to leave the page?'; // a space
  (e || window.event).returnValue = confirmationMessage;
  setTimeout(async () => {
    document.getElementById('trello-page').onCloseWindow();
  }, 2500);
  return confirmationMessage;
});

/**
 * @polymer
 * @extends HTMLElement
 */
class TPage extends Polymer.Element {

  static get is() {
    return 't-page';
  }

 ... some properties ....{ colUpdateArr{...}}

 async onCloseWindow() {
    for (var i = 0; i < this.colUpdateArr.length; i += 2) {
      var toCol = this.colUpdateArr[i];
      var fromCol = this.colUpdateArr[i + 1];
      await this.$.query.ref.child(toCol.$key).child('tasks').set(toCol.tasks);
      await this.$.query.ref.child(fromCol.$key).child('tasks').set(fromCol.tasks);
    }
  }

  } //end Polymer.Element

      window.customElements.define(TPage.is, TPage);
  </script>

</dom-module>
3
  • Did you consider saving the data when the user changes it? That way you'd have nothing to do when they choose to close the page. Commented May 14, 2019 at 0:37
  • I tried that ... I am using Sortablejs and it has some challenges if the record is removed from DB. I have raised a stackoverflow question too. I found a workaround for Sortablejs and Firebase polymerfire to work using dummy object that I wish to sync with firebase db. Not the best solution but it worked with constraints which is the above ticket. I update the db on view change but not able to fire the method in on beforeunload event. Commented May 14, 2019 at 7:13
  • Not sure about how (or if) polymerfire exposes it, but I think you're looking for the firebase.database.onDisconnect function: firebase.google.com/docs/reference/js/… Commented Mar 30, 2020 at 18:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.