Skip to content

Core | Navigator issue with 'postPop' event:  #2575

@wcmhc

Description

@wcmhc

Environment

[Core]
  onsenui latest
  
[Platform]
  Desktop - Linux 64-bit

[Browser]
  Emulator - Galaxy Nexus, OS 4.x, API Level 22
  Desktop - Chrome 68.0.3440.106 on Linux 64-bit

Encountered problem
When working with a stack of pages and needing to execute code based on unique data held in the .data attribute of a page, postPop does not return the correct expected information in the leavePage attribute.

Note: The event prePop seems to work as expected, with the page being navigated away from is in leavePage and the page being navigated to in enterPage

How to reproduce

  1. Create multiple pages with unique data parameters
  2. Attach to the postPop event on the ons-navigator and read the data attribute of postPush and postPop when the event fires. They seem to produce the same data

Demo link

  • HTML
<ons-navigator swipeable id="myNavigator" page="page1.html"></ons-navigator>

<template id="page1.html">
  <ons-page id="page1">
    <ons-toolbar>
      <div class="center">Page 1</div>
    </ons-toolbar>

    <p>This is the first page.</p>

    <ons-button id="push-button1">Push page</ons-button>
  </ons-page>
</template>

<template id="page2.html">
  <ons-page id="page2">
    <ons-toolbar>
      <div class="left"><ons-back-button>Page 1</ons-back-button></div>
      <div class="center"></div>
    </ons-toolbar>

    <p>This is the second page.</p>
    
    <ons-button id="push-button2">Push page</ons-button>
  </ons-page>
</template>

<template id="page3.html">
  <ons-page id="page3">
    <ons-toolbar>
      <div class="left"><ons-back-button>Page 2</ons-back-button></div>
      <div class="center"></div>
    </ons-toolbar>

    <p>This is the third page.</p>
  </ons-page>
</template>
  • JS
document.addEventListener('init', function(event) {
  var page = event.target;

  if (page.id === 'page1') {
    page.querySelector('#push-button1').onclick = function() {
      document.querySelector('#myNavigator').pushPage('page2.html', {data: {title: 'Page 2'}});
    };
  } else if (page.id === 'page2') {
    page.querySelector('ons-toolbar .center').innerHTML = page.data.title;
    page.querySelector('#push-button2').onclick = function() {
      document.querySelector('#myNavigator').pushPage('page3.html', {data: {title: 'Page 3'}});
    };
  } else if (page.id === 'page3') {
    page.querySelector('ons-toolbar .center').innerHTML = page.data.title;
  }
  
  document.getElementById("myNavigator").addEventListener('postpush', function(e) {
          console.log(1);
          //These print out correctly as seen when going from Page 2 to Page 3
          if (e.leavePage !== undefined) {
              console.log(e.leavePage.data);
          }
          console.log(e.enterPage.data);
  });
  
  document.getElementById("myNavigator").addEventListener('postpop', function(e) {
          console.log(2);
          //These two have the same data.
          //Expected outcome is the leavePage to be Page 3 and enterPage to be Page 2, but both return Page 3 (or whatever the previous `pop`'d page happens to be)
          console.log(e.leavePage.data);
          console.log(e.enterPage.data);
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions