PDA

View Full Version : Get element/item based on it's xtype



wallynm
30 Nov 2011, 8:09 PM
I have this code, he look's inside an component if some xtype already exists...



var view = 'Default';
var target = getCmp('content');

// Load the view inside the var
if (Ext.isString(view)) view = this.getView(view);

var element = target.child(view.xtype);
if(element) target.remove(element);

but i having trouble because, the Ext can't fount the xtype inside the target...

i know that it could be done this using the getCmp Instead of xtype to see if it already exist on the page, but i don't wanna to make it because i will need to work with too many id's on the system and it can bring troubles on the future.

Needing help on this guys, thanks! :)

wallynm
30 Nov 2011, 9:42 PM
Ok so, i still need the answer for the first question, but if anyone could answer me, if the code panel.add could put multiple elements inside the HTML, would help me...

My problem actually, it is i have this code:



init: function() {
this.control({
'SidebarCategorias': {
itemclick: function(){
this.renderPanel('center', 'myView');
console.warn('Item clicked')
}
}
});
},


When this code runs, the item click turns on, and the console warn its printed, but when i load an view, them load it again, it print two console warns... I tried to use removeAllListeners but i think i didnt put it in the right place, if someone could help me... :)

twaindev
1 Dec 2011, 2:54 AM
Try something like this for your first question:


var element = target.child('[xtype=panel]');

This should find the first child with a xtype of panel.

twaindev
1 Dec 2011, 3:00 AM
For your second question:

Handlers registered with control() are handled by the application's EventBus and AFAIK can not be unregistered. You'll have to restructure your code so the init function is called only once.

wallynm
1 Dec 2011, 5:28 AM
For your second question:

Handlers registered with control() are handled by the application's EventBus and AFAIK can not be unregistered. You'll have to restructure your code so the init function is called only once.

@twaindev There's an way to avoid the system to register an new control to the item? Like checking if the item already has an listener or something else?

Also, if i remove the element from my page, the listener continue registered?

wallynm
1 Dec 2011, 5:34 AM
Ok, i solved the problem about the clicks on my page, doing this:

if(this.controlBase== 1){
this.control = 1;

this.controlBase({
'SidebarCategorias': {
itemclick: function(){
this.renderPanel('centro', 'Financeiro.Categorias.Teste');
console.warn('Item clicked')
}
}
});
}

Control base it's just an counter, which update when control its instantiated in layout.

But i don't wanna to keep with this code, it is a mess, if i could see if the target has listeners woul be really better. i will try to lookm for something on the Docs, but if someone know an way to do it.
has an card panel, but my left panel, i cant remove it's content...

Before debugging, and debugging, the only way to solve this problem will avoiding the control to be appended more than once... Still need the answer, Someone there? _o/

wallynm
1 Dec 2011, 6:51 AM
+1

twaindev
1 Dec 2011, 7:09 AM
Your code above is indeed ugly. The only way to avoid this is to restructure your program. Since you have not told us exactly what you want to accomplish it is difficult to advise.

wallynm
1 Dec 2011, 4:30 PM
@twaindev - You was right, you couldn't help me more, because i didn't give you all the code that was need to discover the bug... Well, i will try to improve my questions...

Well, Finally i discover my problem, in my system, i'm using the getController, also, im firing it's init event when it is loaded, but the problem was every time that getController was called, the init function was called too, them my control was binding and binding my events every time that it was called.... This was the code

My code was like this:

controller = app.getController(match.controller);
controller['init'].call(controller, params);

Every time that an cotroller was loaded, the init was called too
so i changed it to this



if(app.controllers.keys.indexOf(match.controller) == -1){
controller = app.getController(match.controller);
controller['init'].call(controller, params);
} else {
controller = app.getController(match.controller);
}

First, i check if the controller was loaded on the system, if false (-1)

if(app.controllers.keys.indexOf(match.controller) == -1){
i fire the init method, them, when i need to call it again, i just get the controller without fire the init method... Sorry about all the problems... But finally it is working...The code is much better now, huh? :D