JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2059
Number of posts: 5157

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
return a function when button is pressed Posted by Pud92 on 8 Feb 2009 at 3:18 PM
Hi all, I'm probably missing something terribly simple here but I have this function:

function openFileDialogInstance (ftype, windowId)
{
	var returnId;

	function openFileDialog (ftype, windowId) 
	{
		win = new Window({id: windowId + "-openfile", className: "bluelighting", title: "Open File...", width:300, height:200, destroyOnClose: true, recenterAuto:false}); 
		win.getContent().update("Your " + ftype + " files: <br /> <div id=\"filesopenable\"></div>"); 
		win.showCenter();
	}

	function getFilesOpenable (ftype) 
	{

		var getFilesOpenableRequest = new Ajax.Request (
			"/framework/openfiledialog.php?ftype=" + ftype, {
				method: 'get',
				onComplete: outputGetFilesOpenable
			}
		);
	}

	function outputGetFilesOpenable(output)
	{
        var data = output.responseText.evalJSON();

        for(i=0;i<=data.length;i++)
        {
                      //populate links
        }
        
		//<a href=\"javascript:returnId = $files['id']\">" . $files['name'] . "</a><br />;
		//document.getElementById("filesopenable").innerHTML = responseText;
	}

	openFileDialog(ftype, windowId);
	getFilesOpenable(ftype);
}


I populate the links so that when one is pressed returnId is equal to the number of the link pressed.

My issue is, how to make openFileDialogInstance return returnId when one of those links is pressed?

Thanks in advance for any help :)
Report
Re: return a function when button is pressed Posted by CyGuy on 9 Feb 2009 at 6:08 AM
Hey there, the problem you're having is with variable scope. returnId does not retain it's value and is destroyed after the function openFileDialogInstance executes. Fortunately JavaScript supports object oriented programming and openFileDialogInstance has a global scope.

If you must retain the value declare returnId like this:
this.returnId;

in place of
var returnId;


This will allow you access to the value at any time. Assignment for instance:
openFileDialogInstance.returnId= yourValue;


Happy Programming
Report
Re: return a function when button is pressed Posted by Pud92 on 9 Feb 2009 at 6:24 AM
Ahh that's perfect.

One question: When calling that function would I have to do

openFileDialogInstance();
openFileDialogInstance.returnId= yourValue;


Or could I just do

openFileDialogInstance.returnId= yourValue;


?

Many thanks!
Report
Re: return a function when button is pressed Posted by Pud92 on 9 Feb 2009 at 7:09 AM
Oh actually, how would the caller of the instance know when the returnId is set?
Report
Re: return a function when button is pressed Posted by CyGuy on 10 Feb 2009 at 8:18 AM
oooh! You mention instance. In that regard you may retain the private membership of the variable like this:
function MyClass(){
    var myString = "Hello World";
    this.setMyString = function(isTxt){myString=isTxt;alert(myString)}
}
instance=new MyClass;
...
<a href="javascript:instance.setMyString('Hello Again')">

Above variable instance is defined by the MyClass and may retain it's inherited values!

Otherwise you could use this code:
function MyClass(){
    this.myString = "Hello World"; 
}
function setMyString(isTxt){MyClass.myString=isTxt;alert(MyClass.myString)}
...
<a href="javascript:setMyString('Hello Again')">

The first example is the preferred way of doing things, although the browser sandbox should keep everything private. The second example does expose the myString variable to global scope on the document as does my first example.

Any way it goes the events are fired by the DOM, and the best way to call from the HTML is with a function/method rather than direct assignment. The DOM also allows you to attach events dynamically via javascript. Here it is the assumption that by caller you mean mouse clicks on the document.


WHAT you;see_is_what=you.get;
-Russ aka DangeRuss



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.