Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use relative reference from function

Status
Not open for further replies.

jcaulder

Programmer
Apr 22, 2002
241
US
I am sure this is not nearly as complicated as I'm making it but I can't seem to get anything to work.

I am trying to use relative referencing from within an event handler defined in frame1 of the application. My code looks like this:

.
.
.
this.frmPharmacy.xin_xc.trigger();

var res = function () {
//how do I reference form objects here??
this._parent.frmPharmacy.xin_xc.trigger();
};
this.frmPharmacy.xout_xc.addEventListener("result", res);
.
.
.

I've tried every combination I can think of and nothing seems to work. I know the event handler is firing because I put a trace statement in. How do I reference back to the form objects using 'this.' notation?

Thanks!
 
You are using the XML connector component? Are you also using the dataset component and is that the information you are trying to access in your listener?

Wow JT that almost looked like you knew what you were doing!
 
I am trying to access a wide range of objects from within the a wide range of listeners.

The code above was one example to try to get an idea of how to reference it. I guess they should all be the same relatively from the function. A more complex example would be having "View", "Edit", "Add" buttons on my form. When the user clicks the "View" button, the event listener would fire and cycle through various objects(textinput, etc) on the form setting their enabled property to 'false'.

I cannot get any combination to work from within the listener except absolute referencing which is not what I want.

Why doesn't 'this._parent' work? It would seem the function would be 'this' and the functions' parent would be 'application'.

So I thought

this._parent.frmPharmacy.text_txt.enabled = false;

would reference it.


Thanks!
 
OK... maybe I just don't understand. You are trying to fire a global (function) when you click a button? By form you are referring to data entry right?

How does the XML connector trigger fit in?

Wow JT that almost looked like you knew what you were doing!
 
I have a form in the main application named frmPharmacy. On that form, I have buttons and other objects that I want to define listeners for at the application level so that they are centralized.

In frame1 of the application, I put the following code to define my event handlers and add the listeners to the various objects(instead of the xmlconnector example, I will use a button click example):


//this code is in frame1 of the application
//define event handler for view button
var view = function () {
//how do I reference form objects here??
//i thought something like the following:
this._parent.frmPharmacy.text1_txt.enabled = false;
this._parent.frmPharmacy.text2_txt.enabled = false;
//but this does not work
};

//add event listener to my view button
this.frmPharmacy.view_btn.addEventListener("click", view);

Basically, what I need to know is how to reference objects outside of ANY user defined function, regardless of what the function is doing. I cannot figure out how to reference my form objects without going back to absolute referencing.

Does that make it clearer or did I muddy the waters more?

Thx!
 
That helps a bit. I think I understand now (maybe) :)

So you are saying that frame 1 of your main timeline contains the listener(s) and the buttons and form elements are contained within an MC on the main timeline called "frmPharmacy". That right?

Personally I would right an onRelease function for it. For example:

Code:
//where your listener is
_root.frmPharmacy.viewButton.onClick = function(){
   _root.frmPharmacy.text1_txt.enabled = false;
   _root.frmPhamracy.text2_txt.enabled = false;
}

but if you need to use a listener:

Code:
viewListener = new Object();
viewListener.onClick = function(){
   _root.frmPharmacy.text1_txt.enabled = false;
   _root.frmPharmacy.text2_txt.enabled = false;
}

_root.frmPharmacy.view_btn.addEventListener("onClick",viewListener);

I am pretty sure that onClick is a valid event, but you will have to look it up to verify. (in Help)

Note the listener and the "onClick" event. When you establish your listener it needs to be to the same event that is set in the object.

I was not able to test either of these so some tweeking may be required.

Hope it helps.


Wow JT that almost looked like you knew what you were doing!
 
Isn't using '_root' absolute referencing of the object? Is there no way to use relative referencing such as 'this.something' to refer to _root or frmPharmacy from within the event handler? Isn't 'this' supposed to refer to the object that instantiates the event handler?

So why doesn't 'this.frmPharmacy.text1_txt' referencing work? I can use 'this' referencing everywhere else without problem. It is only the event handler code where all relative referencing breaks down for me.

Thanks for your help!
 
So why doesn't 'this.frmPharmacy.text1_txt' referencing work? I can use 'this' referencing everywhere else without problem.

Maybe I'm reading the code you posted earlier wrong, but you didn't use this.frmPharmacy.text1_txt you used this._parent.frmPharmacy.text1_txt (at least in your example). There is a difference.

I'm not sure I understand why you feel you need that level of relativity... either you know where the MC is or not. One method is relative to the script the other is relative to the main timeline.

If my understanding of your movie is correct I believe the correct reference there would be _parent only.

Code:
_parent.frmPharmacy.text1_txt.enabled = false;

Hope it helps.



Wow JT that almost looked like you knew what you were doing!
 
'this' inside a function refers to the function itself - not the timeline the function is declared on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top