Framework7 插件 API
Framework7 附带简单的插件/扩展API,可让你创建你自己的 Framework7 插件和扩展。
它基于可扩展类。Framework7 中使用的每一个 JavaScript 类(组件)都是可扩展的。
插件结构
首先,让我们看看基本插件的 JS 结构。它基本上是一个对象。
var myPlugin = {
// Module Name
name: 'demo-module',
/* Install callback
It will be executed right after component is installed
Context of this callback points to Class where it was installed
*/
install() {
const Class = this;
console.log(Class);
},
/* Create callback
It will be executed in the very beginning of class initilization (when we create new instance of the class)
*/
create(instance) {
console.log('init', instance);
},
/*
Object with default class/plugin parameters
*/
params: {
myPlugin: {
a: 1,
b: 2,
c: 3,
}
},
/* proto object extends Class prototype */
proto: {
demo() {
return 'demo-module-proto-method';
},
demoStatic: 'demo-module-proto-static',
},
// Extend Class with static props and methods, e.g. Class.myMethod
static: {
demo() {
return 'demo-module-class-method';
},
demoStatic: 'demo-module-class-static',
},
/* Initialized instance Props & Methods */
instance: {
demoProp: true,
demoMethod() {
return 'demo-method';
},
},
/* Event handlers */
on: {
demoEvent(a, b) {
console.log('demo-event', a, b);
},
},
/* Handle clicks */
clicks: {
// prop name means CSS selector of element to add click handler
'p': function ($clickedEl, data) {
// $clickedEl: Dom7 instance of clicked element
// data: element data set (data- attributes)
},
}
};
安装插件
在获取插件后,我们需要将其安装在所需的类上。要安装插件,我们必须在类上调用 .use()
方法。例如,如果这是一个常见的 Framework7 插件
Framework7.use(myPlugin);
必须在类初始化之前(在调用 new Framework7()
之前)安装插件。
演示插件
让我们创建一个简单的调试演示插件。它什么都不做,只是记录一些事件。
/* framework7.debug.js */
var debugEnabled = false;
window.debugPlugin = {
name: 'debugger',
// extend app params with debugger params
params: {
debugger: false,
},
create: function () {
var app = this;
// extend app methods with debugger methods when app instance just created
app.debugger = {
enable: function () {
debugEnabled = true;
},
disable: function () {
debugEnabled = false;
},
}
},
on: {
init: function () {
var app = this;
if (app.params.debugger) debugEnabled = true;
if(debugEnabled) console.log('app init');
},
pageBeforeIn: function (page) {
if(debugEnabled) console.log('pageBeforeIn', page);
},
pageAfterIn: function (page) {
if(debugEnabled) console.log('pageAfterIn', page);
},
pageBeforeOut: function (page) {
if(debugEnabled) console.log('pageBeforeOut', page);
},
pageAfterOut: function (page) {
if(debugEnabled) console.log('pageAfterOut', page);
},
pageInit: function (page) {
if(debugEnabled) console.log('pageInit', page);
},
pageBeforeRemove: function (page) {
if(debugEnabled) console.log('pageBeforeRemove', page);
},
}
}
我们需要将其包含到应用程序中。
<body>
...
<script src="path/to/framework7.js"></script>
<script src="path/to/framework7.debug.js"></script>
<script src="path/to/myapp.js"></script>
</body>
/* myapp.js */
// install plugin first
Framework7.use(debugPlugin);
// init app
var app = new Framework7({
//enable debugger
debugger: true,
});
/*
we can later disable it by calling
app.debugger.disable();
*/