PDA

View Full Version : How to access class properties ?



korndeang
19 Jun 2013, 7:37 PM
I had problem could not access class properties easily, i could access by "this.myProperty" if stay on the same level, but if view class have many items/component nested and nested "this" is belong to current item or component how can i still access property class without use Ext.getCmp ?



Ext.define('myAPP.view.todo.List' ,{
extend: 'Ext.form.FormPanel',
alias: 'widget.todoList',
xtype: 'form',
id : 'todolist-form-widgets',
itemId : 'todolist-form-widgets',
title: false,
autoScroll: true,
frame: false,
collapsible: false,
margins: '0',
border:false,
storeItems:[],
bodyPadding: '0',
border:false,
defaults: {
anchor : '100%',
msgTarget : 'side',
allowBlank: true,
labelWidth: 150,
labelAlign: 'left'
},
initComponent: function() {
cnosole.log(this.id);//Here i could access class property with This.myProperty
this.subfixId = '-'+this.id;
this.callParent(arguments);
},
layout: {
type: 'hbox',
pack: 'center',
align: 'stretch'
},
items: [
{
html:false,
width:'20%',
bodyPadding: '5 10 10 10',
items:[
{
xtype:'panel',
collapsible: true,
margin:'2 2 2 2',
title: 'View',
html:'',
listeners: {
afterRender:function(a,b,c){
// I want access class properties here without use Ext.getCmp(ID)
}
}
},{

}
]

},

Songle
19 Jun 2013, 11:01 PM
In the afterRender listener fire an event at your controller.
From your controller you know all the managed objects and their properties. If you don't have one, you should.

If you REALLY don't want a controller, then put a method on your class and call that from the afterRender ensuring you have kept correct scope.

korndeang
19 Jun 2013, 11:21 PM
In the afterRender listener fire an event at your controller.
From your controller you know all the managed objects and their properties. If you don't have one, you should.

If you REALLY don't want a controller, then put a method on your class and call that from the afterRender ensuring you have kept correct scope.

My purpose user requrest->controller->view (In the view myAPP.view.todo.List it is class ) so as in my sample code above i am already in view myAPP.view.todo.List that i want to got current instance class object to do other purpose operation. If using Ext.getCmp it is possible but my class will modified its ID any time.
The problem in nested items/component i can't use "this"

Songle
19 Jun 2013, 11:43 PM
You are NOT "in" myAPP.view.todo.List when the afterRender listener fires. You are "in" the panel called "View". So you need to find your top level List again.

The best solution is to fire an event at your controller and manage it all from there.

But a quick and dirty solution:



initComponet: function(){
me = this;
},
myFunction: function(){
}
.
.
listeners: {
afterRender: function(a,b,c){
me.myFunction();
}

korndeang
20 Jun 2013, 12:00 AM
Thanks, It works fine, I used to tried similar yours but my mistake is



initComponent: function(){
me = this;
// var me = this; problem with VAR
},
myFunction: function(){ }
. .
listeners: {
afterRender: function(a,b,c){
me.myFunction();
//if i console.log(me) will error unless i remove VAR in initComponent
}