aequery icon indicating copy to clipboard operation
aequery copied to clipboard

aeq.forEach does not work with ScriptUI container children array

Open runegan opened this issue 8 years ago • 4 comments

Original report by Rune Gangsø (Bitbucket: runegan, GitHub: runegan).


Example:

var win = aeq.ui.createWindow()
win.addButton()
win.addButton()
win.addButton()
aeq.forEach( win.get().children, function( key, value ) {
    alert( key + value )
})

This only alerts ones, with the text length3.

Caused by bad checking of array type in aeq.forEach

runegan avatar Jul 27 '17 23:07 runegan

Original comment by Zack Lovatt (Bitbucket: zlovatt, GitHub: zlovatt).


One workflow solution--

aeq.arrayEx(win.get().children).forEach( function( child ) {
    alert( child.text )
});

or to solve the specific issue, we can change aeq.forEach array detection from... if ( obj && Object.prototype.toString.call( obj ) === '[object Array]' )

to

if ( obj && ( Object.prototype.toString.call( obj ) === '[object Array]' || obj.constructor.name === 'Collection' ) )

Thoughts @runegan ?

runegan avatar May 02 '18 18:05 runegan

Original comment by Rune Gangsø (Bitbucket: runegan, GitHub: runegan).


That looks good to me. Does all js objects have the constructor property?

runegan avatar May 02 '18 20:05 runegan

Original comment by Zack Lovatt (Bitbucket: zlovatt, GitHub: zlovatt).


Everything I've seen does, but not sure about everything. Issue is that with prototype.toString.call(whatever) some array-ish elements return [object Array] and others include a string of the elements within them (maybe only ESTK's Collections). My suggestion above seemed robust, but better to double-check.

runegan avatar May 02 '18 20:05 runegan

Original comment by Rune Gangsø (Bitbucket: runegan, GitHub: runegan).


Ok, as long as we don't get a 'something' does not have a property constructor error we're good.

runegan avatar May 02 '18 20:05 runegan