Very new to web development so I apologize for bad terminology or dumb mistakes. BTW I'm using the Microsoft AJAX extensions.
I've create a "class" in javascript using the prototype style. I've attached it to a div that has an image in it using the $create macro. I instantiate multiple instances (2, so far) of this control and everything seems to be working fine. Then, I created a very simple custom event using the EventHandlerList per some samples that I've seen. In this same class I've implemented a handler for this event down in the prototype section of the class definition. I have multiple instances of this class and I need one class to "notify" the other instances about this event. If I step through the code the event is raised and the handler code is initiated as expected. The problem is that references to "this" in the event handler function are basically empty. None of the properties for that class are available. I'm doing plenty of other things with instances of this class and all of the data is there. Any ideas? I've also tried using the "propertyChanged" event and I get exactly the same results. Be glad to share more details if it would help.
Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08) Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08) I want to leave this world the same way I came into it, Screaming, Incontinent & No memory! I laughed that hard I burst my colostomy bag... (\\.\ May03) Life for some is like a car accident... Mine is like a motorway pile up... Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?
Here's some of the code I'm having trouble with. Sorry about the long post but I'm not sure how much of the code might be relevant:
Notes:
At this point this app lets the user drag and drop rectangular "devices" on sections that are configured sort of like a rack mount system.
The event in question is fired in the drop method in the section.js.
The event is received by the onDevDroppedSomewhere handler. The event data seems to be OK but any references to "this" in the handler seem to be invalid or empty.
I'm new at this but I'm expecting that this handler is in an instance (1 of 2) of a section class and that I should be able to reference and update properties of this instance of the class.
I really appreciate any advice on this.
Thanks,
Dave
<script type="text/javascript">
<!--
Sys.Application.add_init(pageInit);
function pageInit(sender, e)
{
// create a section and a couple of devices
$create(DPUIWeb.Section,{}, {}, {}, $get('sectionZone1'));
$create(DPUIWeb.Section,{}, {}, {}, $get('sectionZone2'));
// create an array to hold the sid of the device in each cell
this._devices = new Array();
for (var ndx=1; ndx<=24; ndx++)
{
Array.insert(this._devices, ndx, '0000');
}
canDrop : function(dragMode, dataType, dragData)
{
// make sure it's a device
if (dataType != '__device')
return false;
// get the current y offset
this._dropLocation = Sys.UI.DomElement.getLocation(dragData.control._dragVisual);
return this._isCellEmpty(this._dropLocation.y, dragData.control.get_height(), dragData.control.get_sid());
},
drop : function(dragMode, dataType, dragData)
{
// notify other sections that the device was just dropped on this section
// in case they need to remove it from their section. this._raiseEvent('devDroppedSomewhere', dragData.control.get_sid());
// update the devices list for this section
var firstCellNo = this._getCellNo(this._dropLocation.y);
var devHeight = dragData.control.get_height()/3;
var lastCellNo = firstCellNo + devHeight - 1;
If its too long then put the file in a post as an attachment OR post the URL to the example as it helps to see the fully rendered pages as well as helps with troubleshooting.
The prototype your using, is it a library you have written or is it one that you have found on the net?
Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08) Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08) I want to leave this world the same way I came into it, Screaming, Incontinent & No memory! I laughed that hard I burst my colostomy bag... (\\.\ May03) Life for some is like a car accident... Mine is like a motorway pile up... Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?
Really appreciate you taking time to look at this.
I've attached a file called DPWeb2.zip. I'm using Vista ultimate, Visual Studio 2005 Team Suite, ASP.NET AJAX extensions 1.0 and the ASP.NET AJAX May CTP. The javascript is not from a library but some is loosely based on some sample code in a new book called ASP.NET AJAX in Action by Manning.
When you drag and drop a "device" on a "section" it fires an event that is to notify other sections that a device has been dropped on this section and to remove it from your section if that's where it came from. As I said the section event handler seems to get the event but then all references to 'this' in the event handler do not know anything about the section member variables defined in the constructor.
this refers to the thing its attached to and not "this" in general eg an external variable.
Code:
a = {
id:[],
f:function(){
this.push(f);
}
}
this refers to "a" it is saying a.id.push(f);
when you get to handlers, eg,
Code:
ajax.onreadystatechange = readystatehandler;
and then in the readystatehandler function you refer to "this", this as a property doesnt exist because ajax.onreadystatechange isnt a variable but a method.
so you may want to pass the value of the object your working with to the event handler to attach the correct event trigger to the desired object.
in this case passes the ajaz object to the function so that in the function you make for easier access to that objects methods and properties.
I had a similar experience trying to find the name of an anonymous function, seems that "this" has some latent limitations.
This is the possible reason for the problem, I cant see anything that wrong with the .js but IM only skimming it because theirs allot of code but so far it looks ok.
Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08) Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08) I want to leave this world the same way I came into it, Screaming, Incontinent & No memory! I laughed that hard I burst my colostomy bag... (\\.\ May03) Life for some is like a car accident... Mine is like a motorway pile up... Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?
Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08) Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08) I want to leave this world the same way I came into it, Screaming, Incontinent & No memory! I laughed that hard I burst my colostomy bag... (\\.\ May03) Life for some is like a car accident... Mine is like a motorway pile up... Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?
Bookmarks