PDA

View Full Version : Question to extjs gurus



Bobrovnik
1 Oct 2009, 11:41 AM
Hi all


Recently I have faced with such situation. I have a component, for example


Component1 = Ext.extend(Ext.GridPanel, {
basicButtons: [],
initComponent: function(){

for(var I = 0; I < this.initButtons.length; I ++)
this.basicButtons.push(this.initButtons[i]);
….
Ext.apply(this, {
tbar: this.basicButtons
});
….
}
….
})

Then I call this component twice, for example



new Component1({
initButtons: [
new Ext.Button({
text: ‘button of instance 1’
})
]
});

new Component1({
initButtons: [
new Ext.Button({
text: ‘button of instance 2’
})
]
});

Then I get two grids but with two buttons in each other instead of one button in avery grid panel. Why?

Adding line



this.basicButtons = [];



to initComponent method solves my problem, I am not sure why it is happening? Could someone give me a peace of theory?

aconran
1 Oct 2009, 2:37 PM
Stanislav -

This is a common problem that many people run into. By putting the basicButtons into the Ext.extend you are putting a complex type (object/array) onto the prototype of the class you are creating. When you do this it will be shared across instances of classes.

By putting the definition of the array inside a constructor or an appropriate template method like initComponent you are guaranting that this will be created as an instance level variable.

Bobrovnik
1 Oct 2009, 8:38 PM
thanks for information Aconran

Bobrovnik
2 Oct 2009, 1:20 AM
Thanks for information , Aconran