Yellen Yellen - 1 year ago 225
Javascript Question

ExtJs 5.0 : forEach on Array of Objects not working in IE8

I've a simple model -

Ext.define('MyModel', {
extend: 'Ext.data.Model',

requires: [
'Ext.data.field.String'
],

fields: [
{
type: 'string',
name: 'currentA'
},
{
type: 'string',
name: 'currentB'
},
{
name: 'A'
},
{
name: 'B'
}
]
});


Here is some operation that I'm doing on the data for that model-

onBeforeRender: function(component, eOpts) {
var mystore = Ext.getStore('Mystore');
mystore.load({
callback: function(){
var entity = headerStore.getAt(0);
var bs = entity.data.B;
var as = entity.data.A;
var currentB = entity.data.currentB;
var currentA = entity.data.currentA;

bs.forEach(function(record) {
// do some operations
});
as.forEach(function(record) {
// do some other operations
});
}
});
}


Now, when iterating over variable "bs", which is clearly an array of objects, IE 8 complains

"Object doesn't support this property or method"

for the forEach function. This works fine in Chrome.

Here is the json for the model -

{
"currentA": "a",
"currentB": "b",
"A": [
{
"name": "a",
"id": 1
}
],
"B": [
{
"name": "b",
"id": 2
}
]
}


Why is IE not able identify it as an array?

Answer Source

You can use .each from Ext.Array, because IE8 does not support .forEach

Ext.Array.each(bs, function(record) {
});

Ext.Array.each(as, function(record) {
});

[].forEach does not depend on ExtJS version because this is JS Array's method which was added in ES5 standard. From this link you can see which browser supports forEach method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download