【问题标题】:How can I add a method to a singleton in ExtJS4?如何在 ExtJS4 中为单例添加方法?
【发布时间】:2012-04-20 08:34:36
【问题描述】:

这是 ExtJS 形状类

Ext.define('Ext.chart.Shape', {

    singleton: true,

    circle: function (surface, opts) {
        return surface.add(Ext.apply({
            type: 'circle',
            x: opts.x,
            y: opts.y,
            stroke: null,
            radius: opts.radius
        }, opts));
    },
    ...
});

我想为这个类添加一个方法。例如:

myCircle: function (surface, opts) {
        return ...
},

我该怎么做?

【问题讨论】:

  • 4.0.7 现在。有什么不同吗?

标签: extjs extjs4 prototype extjs4.1


【解决方案1】:

我找到了答案。如果有人需要,这就是解决方案:

Ext.define('MyShape', {
    override: 'Ext.chart.Shape',

    initialize: function() {
        this.callOverridden(arguments);
    },

    myCircle: function (surface, opts) {
        return ...
    }
});

【讨论】:

    【解决方案2】:

    您可以扩展以添加新函数,或覆盖以更改类上现有函数的行为。下面的链接是 sencha 文档中解释如何使用它们的详细信息。

    Extend

    Override

    extend 的示例实现:

    Ext.define('yourCustomClassHere', {
        extend: 'Ext.chart.Shape',
        newFunctionName: function () {
           //your function here
        }
    });
    

    【讨论】:

    • 谢谢。你能给我一个例子吗?因为我在尝试使用它们时遇到了不同的错误。
    • 什么错误?我链接到的文档已经包含示例。
    • 我不想扩展它。我只想向形状类添加另一种方法。当我想覆盖它时 -Ext.chart.Shape.override({})- 我得到了这个错误:“Ext.chart.Shape.override 不是一个函数”
    • 如果我想扩展它,我得到这个错误:“parentPrototype is undefined”
    • 添加了扩展的示例实现。我相信 Extend 用于添加新功能,而 Override 用于更改现有功能的行为。
    猜你喜欢
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多