0

Sorry for the vague/unclear title but I could not think of a 'one line says it al' kind of title. My problem is that I've loaded data from a database into several arrays. With a forloop I create several buttons with the following code:

productButtons[i] = Titanium.UI.createView({
    width:"100%",
    height:"90",
    top:90*i,
    link:positionInArray,
    backgroundImage:"/images/menu/itemBackground.png",
    backgroundSelectedImage:"/images/menu/itemBackground-over.png"
});

productButtons[i].addEventListener('click', function(e) {
    var productLocation = {link:e.source.link};
    var productScrn = Alloy.createController('product',productLocation).getView();
    productScrn.open();
});

I've read somewhere that you could create your custom vars and add them to a view, like I do above with 'link'. So when the button (or view) is clicked, I try to retrieve the link of the view by storing it in the productLocation var using e.source.link.

I capture the passed var in the productScrn using:

var args = arguments[0] || {};
var productLocation = args.link;

But when I log or alert the new productLocation variable in the newly opened screen, 9 out of 10 times it returns undefined. How can this be?I've tested with replacing e.source.link for a number which does seem to work. So is my custom variable in the createView incorrect?

Cheers!

Tomjesch
  • 492
  • 4
  • 27
  • you way to pass value is proper, try logging the value of `productLocation` value in click listerner. – turtle Nov 14 '14 at 04:28
  • I've added 5 logs, in order to track the placement of the link from the button creation to the moment it is received in the product.js: `Ti.API.log(positionInArray + " = the link received from array");` `Ti.API.log(productButtons[i].link + " = the link placed in view");` `Ti.API.log(e.source.link + " = the link of the source");` `Ti.API.log(productLocation.link + " = the link received in eventListener");` `Ti.API.log(receivedProduct + " = the link received in product.js");` Sometimes all logs are correct and sometimes all are undefined... – Tomjesch Nov 14 '14 at 07:35
  • Though the positionInArray (which I use when creating the views) always logs correctly (only logs when a button is created) . – Tomjesch Nov 14 '14 at 08:22
  • this maybe a problem of [Hoisting](http://thecomputersarewinning.com/post/a-dangerous-example-of-javascript-hoisting/) , once try wrapping your click listener in self-executing function like `(function(){productButtons[i].addEventListener('click', function(e) { var productLocation = {link:e.source.link}; var productScrn = Alloy.createController('product',productLocation).getView(); productScrn.open(); }); }());` – turtle Nov 14 '14 at 08:39
  • So hoisting is using vars before they are declared, right? But I do declare them before using them. Do I place the function right below the button creation, because then I have a function within a function. – Tomjesch Nov 14 '14 at 09:37
  • you should check [closure and hoisting problem](http://stackoverflow.com/q/1451009/3419997) here. – turtle Nov 14 '14 at 09:54

0 Answers0