SyntaxError:函数语句需要ExtJs中的名称
SyntaxError: function statement requires a name in ExtJs
我喜欢从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
}
]
}
当我运行此代码时,在控制台中显示错误。SyntaxError:函数语句需要一个名称loadCheckboxes:function(){
请给我建议这个问题的解决方案。
您似乎正在尝试定义对象的键值:
loadCheckboxes : function(){
// ...
}
但是,这些应该是声明的变量或函数:
var loadCheckboxes = function () {
// ...
};
function loadCheckboxes() {
// ...
}
由于它们不在对象文字{...}
中,其中语法name: value
将定义对象的键集为函数表达式,函数表达式可以是匿名的。
相反,它们是后面跟着函数声明的标签,不能是匿名的。
检查的工作原理
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
});
相关文章:
- 使用Extjs进行函数定义
- ExtJS 4-自定义排序函数只在点击列标题后执行
- 如何在Extjs中覆盖lovcombo中的函数
- 使用 ExtJS 中的函数将元素附加到数组
- ExtJS 4.1或通用Javascript HTTP_授权函数
- 使用ExtJS点击即可调用函数
- extjs-使用传递的参数创建自定义函数
- extjs中事件处理程序函数中的THIS上下文
- 如何在调用 setSource() 函数后重绘或更新 extjs 网格的宽度/高度
- Sencha ExtJs 为什么从“构造函数”方法返回“this”
- ExtJs 4.2, 网格中继来自存储加载事件的事件导致未捕获的类型错误: 未定义不是一个函数
- ExtJS - 如何创建可重用的函数以避免代码冗余
- 在ExtJS控制器中未调用onLaunch函数
- ExtJs 5-在Ext.application中未执行启动函数
- ExtJS:关联的idProperty's转换函数未启动
- 使用ExtJS中的函数
- 调用ExtJS中的函数
- 从动作脚本调用ExtJS函数
- 在Javascript的作用域之外调用ExtJS函数
- Extjs函数改变href值