Odd JS function behavior

2 views
Skip to first unread message

ScottW

unread,
Apr 13, 2007, 11:04:15 AM4/13/07
to mxAjax
Hey gang -

I'm currently trying to use mxAjax to ajaxify a page in my main
project - before, this page had several dependent <select> elements,
and would refresh itself as each element was chosen. Now I've got it
going without reloads, works great - except one minor issue cropping
up that I can't get a handle on.

I have three <select> elements - second one populates based on the
first, and the third based on the second. On the first two, I have
onChange calls in them calling JS queries. Let's call these "choose1"
and "choose2". When you change the first element, "choose1" is called,
and the mxAjax.Select call within that JS function has a postFunction
calling choose2. When you change the second element, choose2 is called
directly. chooose2's mxAjax.Select call has a postFunction as well,
and all that part of it works fine.

The issue here is with the choose1 function - it only seems to really
get called ONCE. Alerts in that function fire off the first time the
first select element is changed - but subsequent changes of that
select element, no alert. BUT - the mxAjax.Select within that function
fires off, all the way through. And even throwing an alert into the
"onChange" bit in the first select - THAT alert only fires the first
time around, and not on subsequent changes. Very bizarre. It's like
the mxAjax part of that function is being cached.

This isn't a huge huge deal, but at the top of choose1 I set a
variable as a flag to indicate to later functions that the first
element has changed, and to do a little extra work - this variable is
getting set the first run through, but not subsequently - so my extra
code is only being executed the one time.

I tried breaking out the variable-set into a separate function to be
called via the first select's onChange, and to have that function set
that variable and then call the main choose1 function - but no
change.Alerts thrown in (even in the onChange) still don't fire.

Firebug in FF isn't reporting any errors.

Any thoughts?

James Holmes

unread,
Apr 13, 2007, 11:53:04 AM4/13/07
to mxa...@googlegroups.com
I think you will have to post some code.


--
mxAjax / CFAjax docs and other useful articles:
http://www.bifrost.com.au/blog/

ScottW

unread,
Apr 13, 2007, 12:10:53 PM4/13/07
to mxAjax
Here's some relevant snippets of code:

(Earlier in the script, I build up two lists of IDs and just spit'em
out into a pair of JS arrays, to be used in "setIssue()" below)
var pubIDList = new Array(#pubList#);
var pubDefIssueIDList = new Array(#issueList#);

(JS code with the function described above described as "choose1".
Ignore the CF vars as params, those work fine)
var diffPub = 0;

function choosePub() {
diffPub = 1;
new mxAjax.Select({
parser: new mxAjax.CFArrayToJSKeyValueParser(),
executeOnLoad: true,
target: "IssueID",
paramArgs: new mxAjax.Param(url,
{param:"DSN=#request.DSN#,pubID={PubID},ShowIDs=#RootFlag#",
httpMethod:"post", cffunction:"getIssues"}),
postFunction: chooseIssue,
source: "PubID"

});
}

(JS function described above as "choose2")

function chooseIssue() {
new mxAjax.Select({
parser: new mxAjax.CFArrayToJSKeyValueParser(),
executeOnLoad: true,
target: "SectionDocumentID",
paramArgs: new mxAjax.Param(url,
{param:"DSN=#request.DSN#,issueID={IssueID},ShowIDs=#RootFlag#",
httpMethod:"post", cffunction:"getSectionsDocuments"}),
postFunction: setIssue,
source: "IssueID"

});
}

function setIssue() {
if (diffPub == 1) {
for (x=0;x <= pubIDList.length; x++){
if (pubIDList[x] == document.curForm.PubID.value) {
$("IssueID").value=pubDefIssueIDList[x];
}
}
diffPub = 0;
}

// now check if secdoc has some real content, and allow the submit
button or not
if ($("SectionDocumentID").value != "0" ) {
document.curForm.submitBtn.disabled = false;
getHalfQueuedList(0);
} else {
document.curForm.submitBtn.disabled = true;
}
}


(then later on, in the form, the <select> elements)

<select name="PubID" id="PubID" onChange="choosePub();">(pre-populated
with data, values matching "pubIDList" array vars above)</select>
<select name="IssueID" id="IssueID" onChange="chooseIssue();"></
select>
<select name="SectionDocumentID" id="SectionDocumentID"></select>

Now if I throw an alert into the top of the choosePub() function, it
fires the first time I change the "PubID", and diffPub is set to 1.
chooseIssue() gets called, then setIssue() - where it finds diffPub =
1, runs through that bit and sets diffPub back to 0.

Any subsequent change of the "PubID" select causes the mxAjax call
within "choosePub()" to fire off, and the subsequent chain of
functions (both JS and mxAjax) all fire off fine - but the diffPub var
remains as 0, which means the first bit of "setIssue()" doesn't fire
off.

Now if there was a better way to use the select functionality of
mxAjax - to be able to tell it 'go get this DB data and populate this
select element over here with the results *and set a certain row as
selected*", I'd be set.

ScottW

unread,
Apr 13, 2007, 1:13:14 PM4/13/07
to mxAjax
Nevermind. :P

Discovered the 'defaultOptions' param for the Select feature. That has
gotten rid of my need to do that 'diffPub' bit altogether, so all is
well.

Reply all
Reply to author
Forward
0 new messages