PDA

View Full Version : How to add ITEMS from a function?



korndeang
1 May 2013, 6:36 PM
I am using Extjs4+ MVC, in my view file add.js i have created class view that contain ITEMS.
by simple and static we could add properties of item by:


...,
items: [
//Simple and static practice
{
fieldLabel: 'Account Name',
xtype : 'textfield',
name : 'acc_account_name',
itemId : 'acc_account_name',
allowBlank: false,
emptyText : 'Enter a Account Name',
}
],
...

my problem here:


Ext.define('CRM.view.account.Add' ,{
extend: 'Ext.form.FormPanel',
alias: 'widget.accountAdd',
xtype: 'form',
id : 'account-form-add-widgets',
itemId : 'account-form-add-widgets',
autoScroll: true,
frame: true,
collapsible: true,
margins: '5 5 5 5',
storeItems:[],
tools: [
{type:'close'}
],
bodyPadding: '10 20',

defaults: {
anchor : '90%',
msgTarget : 'side',
allowBlank: true,
labelWidth: 150,
labelAlign: 'left'
},
title: 'Add New Account',
initComponent: function() {
this.callParent(arguments);
},

items: [this.formaddfield()],//Error here "TypeError: this.getFormaddfield is not a function". I want call function in here, how?
formaddfield: function() {// I create function here because i can add dynamic item properties from database

var t= {
fieldLabel: 'Account Name',
xtype : 'textfield',
name : 'acc_account_name',
itemId : 'acc_account_name',
allowBlank: false,
emptyText : 'Enter a Account Name',
}
console.log(Ext.getStore('Account'));
return t;
}
});

skirtle
2 May 2013, 12:12 AM
The JavaScript engine will call that function immediately, before it even calls Ext.define. At that point the this reference is pointing to the global window object.

Assign the items in your initComponent instead, before you invoke callParent, and it should work fine.


initComponent: function() {
this.items = ...;

this.callParent();
}