Need some help synch'ing outer loop counter with dialog.onconfirm()

I am writing a game for Facebook. IN the following code, I have a problem. I have a for loop executing, and in that loop, I call a dialog and implement 'onconfirm' for the dialog. The problem is that I need to access th e loop counter inside of the onconfirm function. But because the onconfirm is called outside of the scope of the for loop, the counter value is no longer valid because it's been incremented. I need some way to pass the counter value to the dialog onconfirm as it was at the time the dialog was displayed, not after the loop has finished. Or maybe someone has a better solution. Any help would be appreciated. Thanks.

function unloadCargo() { //debugger; var actionPrompt = document.getElementById('action-prompt'); actionPrompt.setTextValue('Unloading cargo...'); var ajax = new Ajax(); ajax.responseType = Ajax.JSON; ajax.ondone = function(data) { debugger; if(data.unloadableCargo.length == 0) { loadCargo(); } else { //console.log('unloadable cargo='+dump(data.unloadableCargo)); var i = 0; var j = 0; var ucCount = data.unloadableCargo.length; for(i = 0; i < ucCount; i++) { cargoDialog = new Dialog(); cargoDialog.showChoice('Unload Cargo', 'Unload ' + data.unloadableCargo[i].goods_name + ' at ' + data.unloadableCargo[i].city_name + ' for ' + data.unloadableCargo[i].payoff + 'M euros?'); cargoDialog.onconfirm = function() { //console.log('unloadable cargo onconfirm='+dump(data.unloadableCargo)); var ajax = new Ajax(); var param = {"city_id": data.unloadableCargo[i].city_id, "goods_id": data.unloadableCargo[i].goods_id, "payoff": data.unloadableCargo[i].payoff}; ajax.ondone = function(demandData) { var demands = document.getElementById('demands'); var innerXhtml = '<span>'; for(var j = 0; j < demandData.demands.length; j++) { innerXhtml = innerXhtml + ' <div class="demand-item"><div class="demand-city">' + demandData.demands[j].city + '</div><div class="demand-pay">' + demandData.demands[j].cost + '</div><div class="demand-goods">' + demandData.demands[j].goods + '</div></div>'; } innerXtml = innerXhtml + ' </span>'; demands.setInnerXHTML(innerXhtml); // update balance loadCargo(); } + "/turn/do-unload-cargo", param); } cargoDialog.oncancel = function() { loadCargo(); } } //loadCargo(); } } + '/turn/unload-cargo'); }

-------------Problems Reply------------

You need to pass the value to the dialog somehow.

I have never looked at the FBJS, but it seems setContext can be used for that.

Try this:

cargoDialog = new Dialog().setContext({currentIndex: i});
// showChoice is the same
cargoDialog.onconfirm = function() {
alert(this.currentIndex); // Here you should be able to get it

Category:javascript Views:3 Time:2009-10-21

Related post

  • Python loop counter in a for loop 2009-07-26

    In my example code below, is the counter = 0 really required, or is there a better, more Python, way to get access to a loop counter? I saw a few PEPs related to loop counters, but they were either deferred or rejected (PEP 212 and PEP 281). This is

  • real time loop counter ouput wpf 2009-11-24

    What can I do if I want to have a text-box representing in real time the value of a loop counter in wpf? appending working solution: public partial class Window1 : Window { public Window1() { InitializeComponent(); } private delegate void UpdateTextB

  • Is it better to use integers as loop counter variables? 2010-02-11

    I remember reading somewhere that it is better to use integers as loop counter variables rather than char or short. If yes, why? Does it provide any optimization benefits? --------------Solutions------------- Generally, the compiler will make int to

  • Loop counter in Java API 2010-11-15

    All, While going through some of the files in Java API, I noticed many instances where the looping counter is being decremented rather than increment. i.e. in for and while loops in String class. Though this might be trivial, is there any significanc

  • CakePHP Model index array needed, but can't generate from for loop counter. Need model indexed array for this to work 2011-02-11

    I am working on a function that submits multiple records on various relationship types. The main issue I am running into is the format of the array. In order for my saveAll() to work on my multiple relationships setup, the array needs to be in this f

  • Python CGI Script (using XML & mindom) does not print FOR loop counter 2011-07-18

    Trying to print the results of a Yahoo search using an API, the For loop counter will not print its incremented value. The XML is parsed and printed, but the counter prints '1' over and over. The same code works fine for a Bing API. for counter1 in r

  • Voiceglue Logger says Maximum loop count exceeded. There is probably an infinite loop of in your VXML document 2011-11-09

    Can Any please explain why this is happening. what are the possibilities of errors that are been counted as I have set maxerrorcount = 3 EROR OPEN_VXI luke---- callid=[68] |1098905920|68|CRITICAL|com.vocalocity.vxi|216|VXIinterpreterRun: Maximum loop

  • xslt loop count context, value from top xml 2012-01-02

    <Root> <Envelope> <Header> <ineed>apple</ineed> </Header> <success></success> </Envelope> <Envelope> <Header> <ineed>apple</ineed> </Header> <success></succe

  • JMeter Loop Count help needed 2012-02-03

    I want to run a JMeter test with a number of concurrent threads with each thread sending a request every 10 seconds. These are my thread properties. Number of Threads: 10 Ramp-Up Period: 10 Loop Count: 1 Result: 10 requests divided over 10 seconds, s

  • How do you get the loop counter/index using a for-in syntax in JavaScript? 2012-04-16

    EDIT: Don't use for-in to iterate over an Array, use it to iterate over the property of an object. I will keep this answer up as a reference. Thanks all! I understand the basic for-in syntax for a loop in JavaScript looks like this: for(var obj in my

  • Outlook 2010 "Sent" forlder is not synch'ing with my Verizon/Yahoo web based mail. 2013-03-13

    I have just installed Outlook 2010 on Windows 7 OS, and finished setting up my Verizon/Yahoo mail on it, but cannot get the "Sent" folder from Outlook to synch with Verizon's mail. Please note that when I originally set-up the Verizon email account i

  • Get loop count inside a Python FOR loop 2010-07-01

    In a Python for loop that iterates over a list we can write: for item in list: print item and it neatly goes through all the elements in the list. Is there a way to know within the loop how many times I've been looping so far? For instance, I want to

  • Python for-loop counter error 2010-07-26

    I am attempting to script a short code to figure out the number of days it takes to reach a given principal in the bank due to daily interest. Using my code below does not yield any errors when run in IDLE, but the counter returns 0. Any ideas what I

  • javascript: Using the current for-loop counter-value inside a function() { }? 2010-08-10

    on a website i want to do this: (simplified) myHandlers = new Array(); for(var i = 0; i < 7; i++) { myHandlers.push(new Handler({ handlerName: 'myHandler'+i, // works, e.g. ->myHandler1, 2, 3 etc. handlerFunc: function(bla) { /*...*/ alert(i);

  • Please explain closures, or binding the loop counter to the function scope 2011-01-23

    I've seen programmers assign events listeners inside loops, using the counter. I believe this is the syntax: for(var i=0; i < someArray.length; i++){ someArray[i].onclick = (function(i){/* Some code using i */})(i); } Could someone please explain

  • How to add loop counter to foreach in csh 2011-02-22

    In CSH foreach loop or for loop, how can I add a loop iterator or counter which increases from 10 to 1000 with steps of 20? Something like foreach i (1..20..5) or for (i=1;i<20;i++). --------------Solutions------------- If you have the seq command

  • Loop counter & pointers 2011-06-13

    I'm writing a small specialized C99 library for graphs and I often get loops of the form: for(int i = 0; i < graph->nvertices; ++i) { // ... } I'm wondering if it's a good practice, especially in the case of tigh loop. At first I thought the co

  • Increasing the value of a for loop counter 2011-10-20

    Is there any programming language/script who won't allow increasing the counter for an FOR loop inside the loop? For example: for(int i = 0; i < 10; i++) { i++; print i } Output: 1 3 5 7 8 9 --------------Solutions------------- Not sure I fully un

  • loop counting to 100 and back to 0 and up to 100 again and so on 2011-11-29

    I must admit, I am not a mathematical expert, thats why I cant solve the following problem to my satisfaction. I have a number, say i=0. I have a function that increases i on each call by 1 and then calls itself again, increasing i another time and a

Copyright (C), All Rights Reserved.

processed in 0.153 (s). 11 q(s)