我刚刚开始在 Ext.app.Application 中使用 init 方法,它声明它是一个模板化方法,对于启动和其他一些方法也相同。该文档附有一个带有灰色背景的标签,上面写着“模板”。
什么是模板方法?它与其他方法有何不同?
我需要担心吗?
模板方法是一种可以在派生类上实现的方法,就像您想要的那样,然后自动调用。不用担心,这是您会经常做的事情。
如果您查看文件的源,您会发现它已经存在,但分配了一个空函数。框架正在调用该空函数。如果您从具有模板方法的类扩展,您可以“覆盖”这些方法。框架按一定顺序调用这些方法。
Ext.app.Controller的一段源代码:
/**
* A template method that is called when your application boots. It is called before the
* {@link Ext.app.Application Application}'s launch function is executed so gives a hook point
* to run any code before your Viewport is created.
*
* @param {Ext.app.Application} application
*
* @template
*/
init: Ext.emptyFn,
/**
* A template method like {@link #init}, but called after the viewport is created.
* This is called after the {@link Ext.app.Application#launch launch} method of Application
* is executed.
*
* @param {Ext.app.Application} application
*
* @template
*/
onLaunch: Ext.emptyFn,