Events
It allows us easily to emit and handle all kind of events, including events between components.
When you create app instance or any other component using API, you can pass event handler on app/component initialization in parameter:
Add Event Handlers
var app = new Framework7({/*...*/});
var popup = app.popup.create({/*...*/});
app.on('pageInit', function (page) {
// do something on page init
});
app.on('popupClose', function (popup) {
// do something on popup open
});
popup.on('open', function (popup) {
// do something on popup open
// Once handler, will work only once
popup.once('close', function (popup) {
// do something on popup close
});
Add Multiple Handlers
We can pass multiple events in first parameters separated with space:
app.on('popupOpen popupClose', function (popup) {
// do something on popupOpen and popupClose
});
Remove Event Handlers
Named function handler can be removed:
Remove All Handlers
// Remove all tabShow handlers
Emit Events
And of course we can emit events and any kind of custom events we may need:
app.on('myCustomEvent', function (a, b) {
console.log(a); // -> 'foo'
console.log(b); // -> 'bar'
app.emit('myCustomEvent', 'foo', 'bar');
Events which emitted on components will delegate to app instance as well:
app.on('something', function () {/*...*/});
popup.on('something', function () {/*...*/});
popup.emit('local::something'); // will only trigger "something" event assigned to popup instance above
Event handler context (this
) will always point to instance where it was assigned:
app.on('popupOpen', function () {
console.log(this); // -> app instance
});
popup.on('popupOpen', function () {
console.log(this); // -> popup instance