简体   繁体   English

ExtJS:如何从重写中调用原始方法?

[英]ExtJS: How to call original method from an override?

How do I call the original method from an override method? 如何从覆盖方法调用原始方法?

I have a combobox from which I am removing one of the values from its store to prevent users from selecting it due to the fact that we are no longer supporting that value in that value. 我有一个组合框,由于我们不再在该值中支持该值,因此将从其存储中删除其中一个值,以防止用户选择它。 I still want that value to be displayed properly if the combobox receives it, because technically, it's not an invalid value; 如果组合框收到该值,我仍然希望该值能够正确显示,因为从技术上讲,它不是无效的值。 it's just no longer supported. 它不再受支持。 In order to achieve my goal, I want to override the getDisplayValue() method such that, if the combo box receives the value that is no longer in the store, I want the override method to return the correct string, but if it receives any other value, I want the original method to handle it, like so: 为了实现我的目标,我想重写getDisplayValue()方法,这样,如果组合框接收到存储中不再存在的值,我希望重写方法返回正确的字符串,但是如果它接收到任何内容,其他值,我希望原始方法可以处理它,如下所示:

myCombobox = Ext.create("Ext.form.field.ComboBox",
  {
    // <snip><snip>
    getDisplayValue: function()
      {
        if (this.value == 'removedValue')
          {
            return 'Correct Text';
          }
        else
          {
            // What do I do here to call original getDisplayValue() and return its returned value?
          }
      }
  });

Update 更新
Someone posted an answer which said to use this.callParent(arguments); 有人发布了一个答案,说要使用this.callParent(arguments); but then they deleted the answer after I left a comment saying that that didn't work. 但是在我发表评论说那行不通之后,他们删除了答案。 I got the override function to do what I want it to do in the else case by putting in the source code from the overridden function (which I got from Sencha's web site), but I'd rather use a solution that involves somehow actually calling that function instead if that's possible, as its source code could change in a later ExtJS update (eg, for a bug fix), while mine would remain static. 在其他情况下,我可以通过重写函数的源代码(从Sencha网站获得)来实现重写函数的功能,但是我宁愿使用一种涉及实际调用的解决方案如果可能的话,可以使用该函数,因为它的源代码可以在以后的ExtJS更新中更改(例如,用于错误修复),而我的代码将保持静态。

(Note that I changed the code slightly to look at the value instead of the rawValue, since the latter isn't necessarily defined at the time of the getDisplayValue() call.) (请注意,我稍微更改了代码以查看值而不是rawValue,因为未必在调用getDisplayValue()时定义了rawValue。)

Even though the question is answered, here is another better way to solve your problem. 即使回答了问题,这也是解决问题的另一种更好的方法。 This is how ExtJS calls it parent method in some of its internal classes. 这是ExtJS在其某些内部类中将其称为父方法的方式。

Ext.create("Ext.form.field.ComboBox", { 
    getDisplayValue: function() {
        if (this.rawValue == 'removedValue') {
            // your logic
            return;
        }
        return Ext.form.field.ComboBox.prototype.getDisplayValue.call(this);
    }
});

If you use Ext.define, in 4.1 it was callOverridden , and since 4.2 it is callParent . 如果您使用Ext.define,则在4.1中为callOverridden ,从4.2起为callParent

If you use Ext.create to create a combobox, callParent does not bring you to the combobox's function, but to the function of the base class (triggerfield?), which is not what you want. 如果使用Ext.create创建组合框,则callParent不会带您进入组合框的功能,而是带至基类(triggerfield?)的功能,这不是您想要的。

What I have used successfully once is something like this: 我曾经成功使用过的是这样的:

Ext.create('MyCombo',{
    initComponent:function() {
        var me = this;
        me.callParent(arguments);
        var oldFn = me.getDisplayValue;
        me.getDisplayValue = function() {
            if (this.rawValue == 'removedValue') {
                return 'Correct Text';
            } else {
                oldFn.apply(this, arguments); // What do I do here to call original getDisplayValue() and return its returned value?
            }
        };
    }
});

But it is far cleaner if you use Ext.define to derive your special combobox from the default one and then use callParent . 但是,如果使用Ext.define从默认组合框中派生特殊的组合框,然后使用callParent ,则它要callParent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM