0

我是 CQ5 和 ExtJS 的新手。我创建了一个 cq extjs 小部件。现在我的小部件 js 文件中有很多硬编码的字符串。像 fieldLabel、fieldDescription、addItemLabel、rootPath 等... .

我希望我可以通过从全局变量中读取值来创建另一个单独的 js 文件并声明一些全局变量并设置上述许多字段的值。

我会将这个单独的 js 文件命名为 mywidgetconfig.js,并要求其他团队仅根据他们的需要进行更改,并且仅在此文件中进行更改。

例如,在我的小部件中,我已经硬编码 -->

{
fieldLabel : 'Multi Field to setup links'
}

我希望我可以将此值保留在 mywidgetconfig.js 中:

INNERMULTIFIELD_FIELD_LABEL_TEXT_STRING : 'Multi Field to setup links',

诸如此类

INNERMULTIFIELD_FIELD_DESC_TEXT_STRING : 'blah blah blah'

在我的实际小部件 js 中,我可以将值访问为 -->

{
fieldLabel : MyNamespace.INNERMULTIFIELD_FIELD_LABEL_TEXT_STRING,
fieldDescription: MyNamespace.INNERMULTIFIELD_FIELD_DESC_TEXT_STRING
}

这可能吗?

4

1 回答 1

1

对的,这是可能的。您可以在命名空间中创建变量,然后创建一个新的 js 文件(根据需要创建 mywidgetconfig.js),该文件将仅包含您希望新团队更改的配置。

例如,您可以拥有如下所示的自定义小部件 (customwidget.js),它定义了其命名空间中的变量

/**
 * @class Myns.CustomWidget
 * @extends CQ.form.CompositeField
 * This is a custom widget based on {@link CQ.form.CompositeField}.
 * @constructor
 * Creates a new CustomWidget.
 * @param {Object} config The config object
 */
var Myns = {};
Myns.CustomWidget = CQ.Ext.extend(CQ.form.CompositeField, {

    hiddenField: null,
    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    textf: null,
    /**
     * @private
     * @type CQ.Ext.form.NumberField
     */
    numberf: null,

    constructor: function(config) {
        config = config || { };
        var defaults = {
            "border": true,
            "layout": "form"
        };
        config = CQ.Util.applyDefaults(config, defaults);
        Myns.CustomWidget.superclass.constructor.call(this, config);
    },


    // overriding CQ.Ext.Component#initComponent
    initComponent: function() {
        Myns.CustomWidget.superclass.initComponent.call(this);

        this.hiddenField = new CQ.Ext.form.Hidden({
            name: this.name
        });
        this.add(this.hiddenField);

        this.textf = new CQ.Ext.form.TextField({
            fieldLabel: Myns.TEXTFIELDLABEL, //using variable
            allowBlank: false
        });
        this.add(this.textf);

        this.numberf = new CQ.Ext.form.NumberField({
            fieldLabel: Myns.NUMBERFIELDLABEL, //using variable
            allowBlank: false
        });
        this.add(this.numberf);
    }
    // rest of the code goes here
});
Myns.TEXTFIELDLABEL = "Enter Text"; //defining variable
Myns.NUMBERFIELDLABEL = "Enter a number"; //defining variable
// register xtype 
CQ.Ext.reg('customwidget', Myns.CustomWidget);

你的 mywidgetconfig.js 将包含那些可以被其他人修改的变量。

/*
* Field Label for the text field
*/
Myns.TEXTFIELDLABEL = "New Text"; 

/*
* Field label for number field
*/
Myns.NUMBERFIELDLABEL = "New number"; 

在您的 js.txt 中,确保在您的 customwidget.js 下方添加 mywidgetconfig.js

#base=js
customwidget.js
mywidgetconfig.js
于 2014-03-14T15:09:52.970 回答