I am trying to convert ES6 arrow function to ES5
I have already trying to change it but it lose its scope because I 'm using this.getView()
this.getModel().read('/CharacteristicSet', {
filters: this._afilters,
success: function (oData) {
oViewModel.setProperty('/charSet', oData.results);
for (let i = 0; i < oData.results.length; i++) {
if (oData.results[i].GroupId === sKey) {
oBinding.filter(this._mFilters[sKey]);
}
}
let aIconTabItems = this.byId('iconTabBar').getItems();
let aCharacteristics = oData.results;
for (var j = 0; j < aIconTabItems.length; j++) {
let count = aCharacteristics.filter(
obj =>
obj.GroupId === aIconTabItems[j].getKey() &&
obj.EquipmentNumber ===
this.getView()
.getBindingContext()
.getProperty('EquipmentNumber'),
).length;
oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
}
}.bind(this),
});
I am expecting it to be ES5
One possible solution is to use the thisArg
of Array.filter().
thisArg: Optional - Value to use as
this
when executing callback.
Particularly, and speaking about the arrow function you are trying to convert, you can pass the this
context from the success
callback to be used inside the filter() as this
:
var count = aCharacteristics.filter(function(obj)
{
var equipmentNum = this.getView().getBindingContext().getProperty("EquipmentNumber");
return obj.GroupId === aIconTabItems[j].getKey() && obj.EquipmentNumber === equipmentNum;
}, this /* Here we use the thisArg of filter */).length;
The other way, is just to define a variable outside the loop
, like you are doing with variable aIconTabItems
:
let aIconTabItems = this.byId('iconTabBar').getItems();
let aCharacteristics = oData.results;
let equipmentNum = this.getView().getBindingContext().getProperty('EquipmentNumber');
for (var j = 0; j < aIconTabItems.length; j++)
{
let count = aCharacteristics.filter(function(obj)
{
return obj.GroupId === aIconTabItems[j].getKey() &&
obj.EquipmentNumber === equipmentNum;
}).length;
oViewModel.setProperty(`/${aIconTabItems[j].getKey()}`, count);
}