Page - 图1

Page

Page is one of the main components (containers) used to display app content.

Page Layout

  1. <div class="page" data-name="home">
  2. <div class="page-content">
  3. ... scrollable page content goes here ...
  4. </div>
  5. </div>

Page Name

As you may note, each page has a data-name attribute with a unique page name. It’s not required but can be useful within “page events” or “page callbacks”. It can help us to define which page is loaded and available so you can make required manipulations to the page.

Page Content

All visual content (like list views, forms, etc.) should put inside of <div class="page-content"> which should be a child of <div class="page">. It’s required for correct styling, layout and scrolling.

Page Events

Now lets look at one of the most important parts of page navigation, “page events”. Page Events allow us to manipulate just loaded pages by executing JavaScript for those specific pages:

EventTargetDescription
page:mountedPage Element<div class=”page”>Event will be triggered when new page just inserted to DOM. Or when page with keepAlive route is mounted/attached to DOM
page:initPage Element<div class=”page”>Event will be triggered after Framework7 initialize required page’s components and navbar
page:reinitPage Element<div class=”page”>This event will be triggered in case of navigating to the page that was already initialized.
page:beforeinPage Element<div class=”page”>Event will be triggered when everything initialized and page is ready to be transitioned into view (into active/current position)
page:afterinPage Element<div class=”page”>Event will be triggered after page transitioned into view
page:beforeoutPage Element<div class=”page”>Event will be triggered right before page is going to be transitioned out of view
page:afteroutPage Element<div class=”page”>Event will be triggered after page transitioned out of view
page:beforeunmountPage Element<div class=”page”>Event will be triggered when page with keepAlive route is going to be unmounted/detached from DOM
page:beforeremovePage Element<div class=”page”>Event will be triggered right before Page will be removed from DOM. This event could be very useful if you need to detach some events / destroy some plugins to free memory. This event won’t be triggered for keepAlive routes.

Lets see how we can use these events. There are two ways to add page event handlers:

  1. // Option 1. Using one 'page:init' handler for all pages
  2. $$(document).on('page:init', function (e) {
  3. // Do something here when page loaded and initialized
  4. })
  5. // Option 2. Using live 'page:init' event handlers for each page
  6. $$(document).on('page:init', '.page[data-name="about"]', function (e) {
  7. // Do something here when page with data-name="about" attribute loaded and initialized
  8. })

Page Loading Sequence Classes

When a new page is loading and transitioned into the view (main visible part of the app) it has different classes on page element.

When we load/open new page the following happens:

  1. Currently active page has **page-current** class.
  2. If page we load not in DOM (e.g. loaded via Ajax or using template or from component) it will be added to DOM.
  3. Page that we load/open will have additional **page-next** class set on its element.
  4. Router element (<div class="view">) will have additional **router-transition-forward** class that does the following:
    • page with page-next (new page) class moves into the view
    • page with page-current (current page) class moves out of the view
  5. After transition completed, the new page that we loaded will have **page-current** class
  6. And the page that was previously active will have **page-previous** class

When we load/open previous page (go back) the following happens:

  1. Currently active page has **page-current** class.
  2. If page we go back to not in DOM (e.g. loaded via Ajax or using template or from component) it will be added to DOM.
  3. Page that we go back to will have additional **page-previous** class set on its element.
  4. Router element (<div class="view">) will have additional **router-transition-backward** class that does the following:
    • page with page-previous (page that we go back to) class moves into the view
    • page with page-current (current page) class moves out of the view
  5. After transition completed, the new page that we returned to will have **page-current** class
  6. And the page that was previously active will have **page-next** class. In case this page was added to DOM dynamically, it will be removed from DOM.

Page Data

As you may see it is pretty easy, but how do you determine which page is loaded when we use only one handler? For this case we have Page Data in the event details:

  1. // In page events:
  2. $$(document).on('page:init', function (e) {
  3. // Page Data contains all required information about loaded and initialized page
  4. var page = e.detail;
  5. })

Or in case the event handler was assigned using Dom7 like in example above then it will be passed in second argument:

  1. // In page events:
  2. $$(document).on('page:init', function (e, page) {
  3. console.log(page);
  4. })

Now, in the example above we have page data in **page** variable. It is an object with the following properties:

page.appobjectInitialized app instance
page.viewobjectView instance that contains this page (if this View was initialized)
page.routerobjectRouter instance that contains this page (if this View was initialized). Same as page.view.router
page.appstringInitialized app instance
page.namestringValue of page’s data-name attribute
page.elHTMLElementPage element
page.$elobjectDom7 instance with Page element
page.navbarElHTMLElementRelated navbar element for this page. Available only in iOS theme with dynamic navbar enabled.
page.$navbarElobjectDom7 instance with related navbar element for this page. Available only in iOS theme with dynamic navbar enabled.
page.fromstringPage position before transition or direction of where this Page comes from. It will be next if you load new page, previous - if you go back to this page, or current if this page replaces the currently active one.
page.tostringNew page position or where this page goes to. Can be same next, previous or current
page.positionstringAlias for page.from
page.directionstringDirection of page transition (if applicable). Can be forward or backward
page.routeobjectRoute associated with this page, object with current route data that was used to load this page. It has the following properties
  • url - route URL
  • path - route path
  • query - object with route query. If the url is /page/?id=5&foo=bar then it will contain the following object {id: ‘5’, foo: ‘bar’}
  • params - route params. If we have matching route with /page/user/:userId/post/:postId/ path and url of the page is /page/user/55/post/12/ then it will be the following object {userId: ‘55’, postId: ‘12’}
  • name - route name
  • hash - route URL hash
  • route - object with matching route from available routes
  • context - context that was passed to the route
page.pageFromobjectPage data of the page that was currently active before this new page.
page.contextobjectTemplate7 context that was passed for this page when using Template7 pages

Access To Page Data

If you don’t use page events/callbacks and need to access to page data, it is possible via the f7Page property on its HTMLElement:

  1. var $$ = Dom7;
  2. var page = $$('.page[data-name="somepage"]')[0].f7Page;
  3. // do something with page data