Page
Page is one of the main components (containers) used to display app content.
Page Layout
<div class="page" data-name="home">
<div class="page-content">
... scrollable page content goes here ...
</div>
</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:
Event | Target | Description |
---|---|---|
page:mounted | Page 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:init | Page Element<div class=”page”> | Event will be triggered after Framework7 initialize required page’s components and navbar |
page:reinit | Page Element<div class=”page”> | This event will be triggered in case of navigating to the page that was already initialized. |
page:beforein | Page 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:afterin | Page Element<div class=”page”> | Event will be triggered after page transitioned into view |
page:beforeout | Page Element<div class=”page”> | Event will be triggered right before page is going to be transitioned out of view |
page:afterout | Page Element<div class=”page”> | Event will be triggered after page transitioned out of view |
page:beforeunmount | Page Element<div class=”page”> | Event will be triggered when page with keepAlive route is going to be unmounted/detached from DOM |
page:beforeremove | Page 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:
// Option 1. Using one 'page:init' handler for all pages
$$(document).on('page:init', function (e) {
// Do something here when page loaded and initialized
})
// Option 2. Using live 'page:init' event handlers for each page
$$(document).on('page:init', '.page[data-name="about"]', function (e) {
// Do something here when page with data-name="about" attribute loaded and initialized
})
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:
- Currently active page has
**page-current**
class. - If page we load not in DOM (e.g. loaded via Ajax or using template or from component) it will be added to DOM.
- Page that we load/open will have additional
**page-next**
class set on its element. - 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
- page with
- After transition completed, the new page that we loaded will have
**page-current**
class - And the page that was previously active will have
**page-previous**
class
When we load/open previous page (go back) the following happens:
- Currently active page has
**page-current**
class. - 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.
- Page that we go back to will have additional
**page-previous**
class set on its element. - 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
- page with
- After transition completed, the new page that we returned to will have
**page-current**
class - 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:
// In page events:
$$(document).on('page:init', function (e) {
// Page Data contains all required information about loaded and initialized page
var page = e.detail;
})
Or in case the event handler was assigned using Dom7 like in example above then it will be passed in second argument:
// In page events:
$$(document).on('page:init', function (e, page) {
console.log(page);
})
Now, in the example above we have page data in **page**
variable. It is an object with the following properties:
page.app | object | Initialized app instance |
page.view | object | View instance that contains this page (if this View was initialized) |
page.router | object | Router instance that contains this page (if this View was initialized). Same as page.view.router |
page.app | string | Initialized app instance |
page.name | string | Value of page’s data-name attribute |
page.el | HTMLElement | Page element |
page.$el | object | Dom7 instance with Page element |
page.navbarEl | HTMLElement | Related navbar element for this page. Available only in iOS theme with dynamic navbar enabled. |
page.$navbarEl | object | Dom7 instance with related navbar element for this page. Available only in iOS theme with dynamic navbar enabled. |
page.from | string | Page 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.to | string | New page position or where this page goes to. Can be same next , previous or current |
page.position | string | Alias for page.from |
page.direction | string | Direction of page transition (if applicable). Can be forward or backward |
page.route | object | Route associated with this page, object with current route data that was used to load this page. It has the following properties
|
page.pageFrom | object | Page data of the page that was currently active before this new page. |
page.context | object | Template7 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:
var $$ = Dom7;
var page = $$('.page[data-name="somepage"]')[0].f7Page;
// do something with page data