如何从 ExtJS 控件引用祖先容器对象?

发布于 2024-11-06 12:35:11 字数 1179 浏览 3 评论 0原文

我正在使用 ExtJS 构建一个包含多个面板作为项目的窗口。其中一个面板包含一个按钮。

我想将一个处理程序附加到我的按钮,这样当我单击该按钮时,我可以隐藏包含上述面板和该按钮的窗口。

我的问题是:如何在不通过 id 引用窗口的情况下获取对按钮父窗口的引用?我确实想要引用 Ext.Window 实例,而不是包含我的按钮的 Ext.Panel 实例。

注意:我不想通过 id 引用窗口,因为我是 Ext.Window 类的子类,因此窗口的 id 不会始终相同。简而言之,我正在创建一个向导类,当我单击向导的取消按钮时,我想隐藏包含该按钮的向导窗口。

这是我的代码:

var newWizardWindow = new WizardWindow({
  id: 'newWizardWindow',
  title: 'New',
  items: [
    ...
  ],   
  buttons: [{
    text: 'Cancel',
    handler: function() {
      // REFERENCE WizardWindow instance here.
    }
  },{
    id: 'newWizardPreviousButton',
    text: '« Previous',
    disabled: true,
    handler: newWizardNavigator.createDelegate(this, [-1])
  },{
    id: 'newWizardNextButton',
    text: 'Next »',
    handler: newWizardNavigator.createDelegate(this, [1])
  }],
  listeners: {
    …
  }
});

以下是我就如何隐藏窗口提出的一些想法:

  1. this.ownerCt.ownerCt(这是按钮)。不利的是,随着 ExtJS 的未来更新,窗口和按钮之间的父项数量可能会发生变化。
  2. 以某种方式在 WizardWindow 类中存储对 WizardWindow 实例的引用。
  3. 以 jQuery 方式查找最接近的 WizardWindow [CSS] 类:$(this).closest('.wizardWindow')。也许 this.findByParentType('WizardWindow') ?

I am using ExtJS to build a window containing several panels as items. One of these panels contains a button.

I want to attach a handler to my button such that when I click the button, I can hide the window containing the above-mentioned panels and this button.

My question is: how can i get a reference to the parent window of my button WITHOUT referencing the window by id? I literally want a reference to the Ext.Window instance and not the Ext.Panel instance that contains my button.

Note: I do not want to reference the window by id because I am subclassing the Ext.Window class, and therefore the window's id will not always be the same. In short, I am creating a Wizard class, and when I click the wizard's Cancel button, I want to hide the wizard window containing the button.

Here is my code:

var newWizardWindow = new WizardWindow({
  id: 'newWizardWindow',
  title: 'New',
  items: [
    ...
  ],   
  buttons: [{
    text: 'Cancel',
    handler: function() {
      // REFERENCE WizardWindow instance here.
    }
  },{
    id: 'newWizardPreviousButton',
    text: '« Previous',
    disabled: true,
    handler: newWizardNavigator.createDelegate(this, [-1])
  },{
    id: 'newWizardNextButton',
    text: 'Next »',
    handler: newWizardNavigator.createDelegate(this, [1])
  }],
  listeners: {
    …
  }
});

Here are some ideas I have come up with as far as how to hide the window:

  1. this.ownerCt.ownerCt (this being the button). Not favorable as with a future update to ExtJS, the number of parents between the window and the button might change.
  2. Somehow store a reference to the WizardWindow instance in the WizardWindow class.
  3. Find the closest WizardWindow [CSS] class in jQuery fashion: $(this).closest('.wizardWindow'). Maybe this.findByParentType('WizardWindow')?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绳情 2024-11-13 12:35:11

尝试一下 findParentByType(...) 方法怎么样?我记得用过几次。

按类型查找Parent(
字符串/Ext.Component/类xtype,
[布尔浅] ) : Ext.Container

找到该组件上方的容器
按 xtype 或类在任何级别。

这里您可以使用 xtype 而不是 id 作为参考,xtype 是无论您拥有哪种类型的实例,都是一样的。

buttons: [{
    text: 'Cancel',
    handler: function() {
        // REFERENCE WizardWindow instance here.
        var parentWindow = this.findParentByType('xtypelizardwindow');
    }
}]

How about trying the findParentByType(...) method? I remember using it a few times.

findParentByType(
String/Ext.Component/Class xtype,
[Boolean shallow] ) : Ext.Container

Find a container above this component
at any level by xtype or class.

Here you can use the xtype instead of the id as reference and the xtype is the same no matter what instance you have of that type.

buttons: [{
    text: 'Cancel',
    handler: function() {
        // REFERENCE WizardWindow instance here.
        var parentWindow = this.findParentByType('xtypelizardwindow');
    }
}]
初见 2024-11-13 12:35:11

您可以将引用作为范围传递。

buttons: [{
    text: 'Cancel',
    scope:this,
    handler: function() {
    }
}]

You can pass the ref as scope.

buttons: [{
    text: 'Cancel',
    scope:this,
    handler: function() {
    }
}]
半城柳色半声笛 2024-11-13 12:35:11

在按钮的配置中,作为处理程序属性的同级,您可以添加一个scope: newWizardWindow 属性吗?我不能 100% 确定这是否有效,但我认为会的。这会将按钮处理程序的范围设置为窗口,并且在处理程序函数内您可以执行 this.hide();

In your configuration for your button, as a sibling to your handler property, can you add a scope: newWizardWindow property? I'm not 100% sure if that will work, but I think it will. This will set the scope of your button handler to be the window, and inside the handler function you can just do this.hide();

护你周全 2024-11-13 12:35:11

this.ownerCt 还将引用父对象,即容器。
因为它使用 this.findParentByType(parent xtype)

this.ownerCt will also gives reference to parent object, which is container.
as it is using the this.findParentByType(parent xtype)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文