2013-02-06 22 views
0

我喜歡在ExtJs中從JSon響應中填充複選框組。當發送按鈕點擊它將在控制檯中顯示響應。SyntaxError:函數語句需要ExtJs中的名稱

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:函數(){

請給我建議對這個問題的解決方案。

+0

一些縮進將是很好 –

+0

'VAR messagePanel =新MyClass的();'和'loadCheckboxes:函數(){'不能有效在一起。一個是變量聲明(在對象文本中無效),另一個是對象文字鍵或標籤+匿名函數聲明(與匿名函數表達式不同,這是不允許的)。 –

+0

爲此提出工作解決方案。 –

回答

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 
}); 
+0

優秀的Diego LEspiñeira。 –

+0

TLDR在解釋 – geotheory

+0

geotheory:穆罕默德烏薩馬要求一個工作的解決方案。這是一個可行的解決方案。 –

3

你似乎是試圖定義對象的鍵值:

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

但是,這些應該作爲聲明的變量或函數:

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

function loadCheckboxes() { 
    // ... 
} 

,因爲他們不是內object literal - {...} - 其中語法name: value將定義對象的密鑰設置爲function expressions,其可以是匿名的。

相反,它們是labels,然後是function declarations,它不能是匿名的。

+0

@MuhammadUsama然後,你應該自己寫一本關於編程的書。這個答案很好解釋得很好 – sra