Empty array after adding elements using Titanium Appcelerator mobile 1.7.2

I'm using Titanium Appcelerator mobile API 1.7.2.

When creating an array, I'm getting some odd results. Is it my syntax?

container.textBoxArray = new Array();
container.textBoxArray[0] = createPasswordTextField(options, '0%');
container.textBoxArray[1] = createPasswordTextField(options, '25%');
Ti.API.log(container.textBoxArray == null);
Ti.API.log('len: ' + container.textBoxArray.length);

The results of the output are 0 (for false) and 'len: 0' respectively. Anyone know why?

Adam

Edit: createPasswordTextField is essentially

function createPasswordTextField(options, left){
    return Ti.UI.createTextField( options... )
}

Answers:

Answer

I've been having problems with Titanium and Arrays too. What you could do, is try this:

container.textBoxArray = [];
container.textBoxArray.push(createPasswordTextField(options, '0%'));
container.textBoxArray.push(createPasswordTextField(options, '25%'));
Ti.API.log(container.textBoxArray == null);
Ti.API.log('len: ' + container.textBoxArray.length);
Answer

the log statement should look like this to view arrays contents

Ti.API.log(JSON.stringify(container.textBoxArray));
Answer

I've come across this as well. When adding an array to a TiProxy object (View, Window, button etc.) it doesn't work as expected. You need to manipulate the array 'off' the proxy, then re-set it. I don't know if this is a bug or just a limitation of properties on TiProxy objects. Here is an example that behaves the same on iOS under Titanium Mobile SDK 1.7.5:

var proxy = Ti.UI.createView();  //this can be any TiProxy object

proxy.someArray = [];
proxy.someArray.push( '1' );
proxy.someArray.push( '2' );
Ti.API.info("Array modified directly on TiProxy object" );
Ti.API.info(proxy.someArray );

var myArray = [];
myArray.push( '1' );
myArray.push( '2' );
proxy.someArray = myArray;
Ti.API.info("Array modified outside TiProxy object" );
Ti.API.info( proxy.someArray );

proxy.someArray.push( '3' );
Ti.API.info("This will be unchanged" );
Ti.API.info(proxy.someArray );

var changeArray = proxy.someArray;
changeArray.push('3');
proxy.someArray = changeArray;
Ti.API.info("This is how you must do it." );
Ti.API.info(proxy.someArray );

returns:

[INFO] Array modified directly on TiProxy object
[INFO] []
[INFO] Array modified outside TiProxy object
[INFO] [ 1,  2 ]
[INFO] This will be unchanged
[INFO] [ 1,  2 ]
[INFO] This is how you must do it.
[INFO] [ 1, 2, 3 ]

Finding out the behavour on Android is a lot harder bacause Ti.API.info(proxy.someArray ); just returns an object reference.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.