0

我喜欢从 ExtJs 中的 JSon 响应填充 checkboxgroup。单击发送按钮时,它将在控制台中显示响应。

Ext.define('myclass', {
extend: 'Ext.form.Panel',
initComponent: function(){
Ext.apply(this, {
renderTo: Ext.getBody(),
title: 'Message',
tbar: [{
    text: 'Send',
    handler: function(){
    console.log(this.getValues());
    },
    scope: this
}],
items: []
});
    this.callParent(arguments);
}
});
    var messagePanel = new myclass();

loadCheckboxes : function(){
Ext.Ajax.request({
    url: 'recipients.json',
    success: function(response){
        console.log(response);
    },
    scope: this
    });
}

this.loadCheckboxes();

onLoad : function(response){
var jsonResponse = Ext.decode(response.responseText);
    if (jsonResponse.success) {
    // success
    console.log(jsonResponse);
    }
}

    Ext.Ajax.request({
    url: 'recipients.json',
    success: onLoad(),
    scope: this
    });

var checkboxGroup = {
xtype: 'checkboxgroup',
columns: 2,
fieldLabel: 'Recipients',
name: 'recipients',
style: {
padding: '10px'
},
items: []
};
this.add(checkboxGroup);

var i, len = jsonResponse.recipients.length, recipient;
for (i = 0; i < len; i++) {
recipient = jsonResponse.recipients[i];
    checkboxGroup.items.push({
    xtype: 'checkbox',
    boxLabel: recipient.fullName,
    name: 'recipients',
    inputValue: recipient.userID,
    checked: recipient.selected
    });
}

这是此代码的 Json 代码。

{
"success": true,
"recipients": [{
"fullName": "Stuart Ashworth",
"userID": 1,
"selected": true
}, {
"fullName": "Andrew Duncan",
"userID": 2,
"selected": false
}
]
}

当我运行此代码时,在控制台中显示错误。 语法错误:函数语句需要名称 loadCheckboxes : function(){

请建议我解决此问题。

4

2 回答 2

3

您似乎正在尝试定义对象的键值:

loadCheckboxes : function(){
    // ...
}

但是,这些应该是声明的变量或函数:

var loadCheckboxes = function () {
    // ...
};

function loadCheckboxes() {
    // ...
}

因为它们不在对象文字中——{...}语法name: value将对象的键定义为函数表达式,它可以是匿名的。

相反,它们是后跟函数声明的标签,不能是匿名的。

于 2013-02-06T06:15:07.710 回答
0

检查这对您有何作用

Ext.define('myclass', {
    extend: 'Ext.form.Panel',
    initComponent: function() {
        Ext.apply(this, {
            renderTo: Ext.getBody(),
            title: 'Message',
            tbar: [
                {
                    text: 'Send',
                    handler: function() {
                        console.log(this.getValues());
                    },
                    scope: this
                }
            ],
            items: [
                {
                    xtype: 'checkboxgroup',
                    columns: 2,
                    fieldLabel: 'Recipients',
                    name: 'recipients',
                    style: {
                        padding: '10px'
                    },
                    items: []
                }
            ]
        });
        this.callParent(arguments);
    },
    onLoad: function(response) {
        var jsonResponse = Ext.decode(response.responseText);
        if (jsonResponse.success) {
            // success
            console.log(jsonResponse);
            var i, len = jsonResponse.recipients.length, recipient;
            var checkboxGroup = this.down('checkboxgroup[name="recipients"]');
            for (i = 0; i < len; i++) {
                recipient = jsonResponse.recipients[i];
                checkboxGroup.add({
                    xtype: 'checkbox',
                    boxLabel: recipient.fullName,
                    name: 'recipients',
                    inputValue: recipient.userID,
                    checked: recipient.selected
                });
            }
        }
    }
});

var messagePanel = new myclass();

function loadCheckboxes() {
    Ext.Ajax.request({
        url: 'recipients.json',
        success: function(response){
            console.log(response);
        }
        // , scope: this <-- you don't need scope here, you are not in a class
    });
}

loadCheckboxes(); // <-- I assume this call and the next are for debugging pourposes

Ext.Ajax.request({
    url: 'recipients.json',
    success: messagePanel.onLoad,
    scope: messagePanel
});
于 2013-02-06T14:27:02.640 回答