0

当我在我的网页中制作一些组件时,我发现那里有很多重复的代码,例如,在我的网页中我想制作两个图表。

它们最大的区别是数据存储和标题,但是它们有许多相同的属性。

我通过定义 Ext.chart 的一个新实例来做到这一点:

var chart1=new Ext.chart.LineChart{config1};

var chart2=new Ext.chart.LineChart{config2};

config1和config2中有很多相同的内容,有什么办法可以避免这种情况吗?

顺便说一句,我想到了 ext 中的扩展机制,但是我无法仅从 ext3.2 API 获得有关如何使用它的更多详细信息。

4

1 回答 1

1

您可以扩展配置

var config = {
    // do you shared config here
}

// and apply any different/extending configs here
var config1 = Ext.apply(config, { title: "Title of config 1" }
var config2 = Ext.apply(config, { title: "Title of config 2" }

var chart1 = new Ext.chart.LineChart(config1);
var chart2 = new Ext.chart.LineChart(config2);

如果你想要它更短:

var chart1 = new Ext.chart.LineChart(Ext.apply(config, {
    title: "Title of config 1"
});
var chart2 = new Ext.chart.LineChart(Ext.apply(config, {
    title: "Title of config 2"
});

编辑:使用 Ext.extend:

Ext.chart.LineChart = Ext.extend(Ext.chart.LineChart, {
    // put your shared config in here
});

var chart1 = new Ext.chart.LineChart({
    title: "Title of chart 1", 
    store: new Ext.data.Store({ ... });
});
var chart2 = new Ext.chart.LineChart({
    title: "Title of chart 2", 
    store: new Ext.data.Store({ ... });
});
于 2010-10-10T12:35:59.357 回答