如何挂接库函数(金色布局)并调用其他方法

问题描述 投票:3回答:2

我正在使用一个名为Golden Layout的库,它具有一个名为destroy的功能,它将在关闭或刷新窗口时关闭所有应用程序窗口

我需要在destroy函数中添加其他方法。我还需要删除所有本地存储。

我该怎么办?请帮助

下面是插件代码。

lm.LayoutManager = function( config, container ) {
....    
destroy: function() {
            if( this.isInitialised === false ) {
                return;
            }
            this._onUnload();
            $( window ).off( 'resize', this._resizeFunction );
            $( window ).off( 'unload beforeunload', this._unloadFunction );
            this.root.callDownwards( '_$destroy', [], true );
            this.root.contentItems = [];
            this.tabDropPlaceholder.remove();
            this.dropTargetIndicator.destroy();
            this.transitionIndicator.destroy();
            this.eventHub.destroy();

            this._dragSources.forEach( function( dragSource ) {
                dragSource._dragListener.destroy();
                dragSource._element = null;
                dragSource._itemConfig = null;
                dragSource._dragListener = null;
            } );
            this._dragSources = [];
        },

我可以像这样访问组件中的destroy方法

this.layout = new GoldenLayout(this.config, this.layoutElement.nativeElement);

this.layout.destroy();`

我的代码

@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
  var originalDestroy = this.layout.destroy;
  this.layout.destroy = function() {
      // Call the original
      originalDestroy.apply(this, arguments);
        localStorage.clear();
  };
}
javascript jquery angular prototype golden-layout
2个回答
1
投票
查看the documentation,GoldenLayout提供了一个itemDestroyed事件,您可以通过该事件进行自定义清理。描述是:

[每当物品被销毁时就被解雇。

如果由于某种原因而不能,通常的答案是,您可以轻松地

wrap函数:

var originalDestroy = this.layout.destroy; this.layout.destroy = function() { // Call the original originalDestroy.apply(this, arguments); // Do your additional work here };
如果需要,您可以通过修改GoldenLayout.prototype对所有实例执行此操作:

var originalDestroy = GoldenLayout.prototype.destroy; GoldenLayout.prototype.destroy = function() { // Call the original originalDestroy.apply(this, arguments); // Do your additional work here };

示例:

// Stand-in for golden laout function GoldenLayout() { } GoldenLayout.prototype.destroy = function() { console.log("Standard functionality"); }; // Your override: var originalDestroy = GoldenLayout.prototype.destroy; GoldenLayout.prototype.destroy = function() { // Call the original originalDestroy.apply(this, arguments); // Do your additional work here console.log("Custom functionality"); }; // Use var layout = new GoldenLayout(); layout.destroy();

1
投票
进入金色布局是events的预期目的。

[@T.J. Crowder简要提到,存在itemDestroyed事件,当布局中的项目被破坏时会调用该事件。

您可以像这样监听此事件:

this.layout.on('itemDestroyed', function() { localStorage.clear(); })


但是,每次

anything被销毁时,都会调用此事件,并且即使关闭标签也将其传播到树下。这意味着,如果在布局根目录上调用destroy,则每个RowOrColumnStackComponent

都会获得一个事件我建议检查传递给事件的项目,如果不是主窗口,则忽略该项目(root项目]

this.layout.on('itemDestroyed', function(item) { if (item.type === "root") { localStorage.clear(); } })

© www.soinside.com 2019 - 2024. All rights reserved.