PDA

View Full Version : How to return results from a store.each loop to another method



kdm
27 Sep 2017, 1:04 PM
Created a class with 2 methods in it. The setEspPrice method calls the second method filterEspStore. The filterEspStore filters the store based on the parameter passed in, which works great. However, when I try to return the result back to the setEspPrice method it does not work. The var this.esp_price is always undefined. When returning a var from inside a store.each function where is the value returned too? How can I send this value back to the setEspPrice method?



setEspPrice: function () {
this.esp_price = this.filterEspStore('manage');
},
filterEspStore: function(filter){
this.storeESP.filter('los', filter);

this.storeESP.each(function (rec) {
if (this.numEmployees >= rec.get('min_users') && this.numEmployees <= rec.get('max_users')) {
return rec.get('monthly_price');
}
}, this);

this.storeESP.clearFilter(true);
}


Thanks in advance!

kdm
28 Sep 2017, 4:55 AM
I've seem to resolve my problem by reorganizing my code. Instead of assigning the esp_price variable in the SetEspPrice method I've decided to set the var in the filterEspStore method. I'm still curious thou on how to return a value from inside a store loop if anyone has an answer.

Thanks,



setEspPrice: function () {
this.filterEspStore('manage');
},
filterEspStore: function(filter){
this.storeESP.filter('los', filter);

this.storeESP.each(function (rec) {
if (this.numEmployees >= rec.get('min_users') && this.numEmployees <= rec.get('max_users')) {
this.esp_price = rec.get('monthly_price');
}
}, this);

this.storeESP.clearFilter(true);
}

petrocomp
1 Oct 2017, 12:10 AM
Hi kdm,

If you want to return a value to setEspPrice function from filterEspStore you have to add return statetment outside each loop. In your example, you have one function inside another (anonymous function inside filterEspStore), so if you add return rec.get('monthly_price') inside anonymous function then you exit only this function not filterEspStore.

I prepare for you another solution for this problem:



Ext.application({
name: 'Fiddle',


launch: function () {
this.storeTest = Ext.create('Ext.data.Store', {
data: [{
firstName: 'Ed',
lastName: 'Spencer'
}, {
firstName: 'Tommy',
lastName: 'Maintz'
}, {
firstName: 'Aaron',
lastName: 'Conran'
}, {
firstName: 'Jamie',
lastName: 'Avins'
}]
});


this.setEspPrice();
},


setEspPrice: function () {
let variable = this.filterEspStore('manage');
alert(variable);
},

filterEspStore: function (filter) {
let returnValue = null;
this.storeTest.each(function (rec) {
if(rec.get('firstName') === "Tommy"){
returnValue = 'This is Tommy!';
}
}, this);


return returnValue;
}
});




Please try it.
If you have any questions please let me know.