Ok, I've found the solution, for anyone having the same problem:
I caught the activate
and beforeactivate
event of the Panel and I retrieve a reference to the list by using an id on the list, (eg: lstProducts
) and then getting it by this.down('#productsList');
.
UPDATE
Here is the template for the list, along with the list and the method that setups the listeners.
The template for the list
productsTemplate = new Ext.XTemplate(
'<tpl for=".">' +
'<table width="100%" cellpadding="0" cellspacing="0"><tr>' +
'<td>' +
'<div class="product">{Name}</div>' +
'</td>' +
'<td rowspan="2" width="10%" align="right">' +
'<table cellpadding="0" cellspacing="0">' +
'<tr><td><div class="btn btnOrder minus" id="btnRemove{#}" > - </div></td> ' +
'<td><input type="text" disabled id="pr-{Id}" value="0" class="productQuantity"/> </td>' +
'<td><div class="btn btnOrder plus" id="btnAdd{#}"> + </div></td></tr></table> ' +
'</td></tr><tr><td>' +
'<table class="orderExtra"><tr>' +
'<td class="sideOrderIcon" id="prSideOrderIcon-{Id}"> </td>' +
'<td valign="middle"><input type="text" disabled id="prSideOrder-{Id}" value="0" class="extraQuantity"/></td>' +
'<td class="extraIcon" id="prExtraIcon-{Id}"> </td>' +
'<td><input type="text" disabled id="prExtra-{Id}" value="0" class="extraQuantity"/></td>' +
'<td class="price">{Price} €</td>' +
'</tr></table>' +
'</td></tr>' +
'</table>' +
'</tpl>'
);
The list itself and the action listeners setup at activate of the view.
productsList = new Ext.List({
store:this.store,
scope:this,
refreshed:false,
id:'productsList',
disableSelection:true,
itemTpl:productsTemplate })
Ext.apply(this, {
layout:'fit',
scroll:'vertical',
items:[productsList],
listeners:{
activate:this.setupQuantityActionListeners
}
At some other part of the code put the below method.
setupQuantityActionListeners:function () {
var panel = this.down('#productsList');
// loop all the list items to add listeners to the buttons
panel.all.elements.forEach(function (item, index) {
//get the button references
var btnAdd = Ext.get('btnAdd' + (index + 1));
var btnRemove = Ext.get('btnRemove' + (index + 1));
//get the product model
var product = app.stores.Products.getAt(index);
var tbxQuantity = Ext.get('pr-' + product.data.Id);
//get the running quantity
if (tbxQuantity)
var quantity = tbxQuantity.getValue();
//add - remove quantity
if (btnAdd) {
btnAdd.on('click', function () {
tbxQuantity.dom.value = ++quantity;
Ext.dispatch({
controller:'Orders',
action:'addOrderItem',
product:product,
quantity:quantity
})
})
}
if (btnRemove) {
btnRemove.on('click', function () {
if (quantity > 0) {
tbxQuantity.dom.value = --quantity;
Ext.dispatch({
controller:'Orders',
action:'addOrderItem',
product:product,
quantity:quantity
})
}
})
}
});
}
Be very careful with the scoping in general. If you want to get something from the view and you try to get it at a component listener method, you must set scope:this
to that component, so as to get a reference of the view when inside the listener method of the component.
Hope it helps