Building JavaScript Behaviors - 3

Posted by: Aspects of AJAX, on 18 Jan 2007 | View original | Bookmarked: 0 time(s)

Methods for event handling

The methods that are used to handle events from the mouse, keyboard or system are identified by their name prefix "on".  When the behavior is bound to the HTML element these methods are not just copied over from the JavaScript behavior declaration to the html element but are wrapped by a special function that looks like

function() {
  return method.apply(htmlElement, arguments);
}

This wrapper is generated automatically for all on__ methods to ensure that the JavaScript "this" pointer is pointing to the htmlElement the method belongs to. This really simplifies writing event code.

Simple Events

The first sample already used an event (onclick) and registered a method to calculate a new random number for the dice.

// classical event handler implementation

onclick: function(evt) { evt = evt || window.event; var src = evt.srcElement; src.rolling = 50; src.count = 0; src.rollNext(); }

Because the this pointer is adjusted to the htmlElement we can use it instead of finding the right element through the event property srcElement:

// simpler event handler implementation

onclick: function(evt) { this.rolling = 50; this.count = 0; this.rollNext(); }

Global Events

Sometimes it is not possible to implement an event code by using this simple on__ naming scheme because the event that the behavior needs is not thrown to the htmlElement of the behavior.

If you are interested in global events you need to attach a method by using the AttachEvent method that is available through the jcl object. Don't use the on___ naming scheme for this method:

jcl.AttachEvent(document, "onmousedown", this._onmousemove);

If you are not interested in these events all the time the handler can be detached by calling:

jcl.DetachEvent(document, "onmousemove", this._onmousemove);

Mouse Events

Mouse events are a little bit special when implementing a drag&drop scenario. The onmousedown will always be raised on the element that will be dragged around but the other 2 events mousemove (while dragging) and onmouseup (when dragging ends and the drop occours) may be raised on any other elent on the page or even on the document object itself. Because the event "bubbles up" we can get all the events by attaichg these 2 methods to the document object.

The sample at VBoxDemo.aspx is using the VBox.js behavior that allows changing the width of the vertical separation by dragging the line between the left and right content.

 

You can download the updated files from http://www.mathertel.de/Downloads/Start_JSBTutorial.aspx.

 

del.icio.us tags: JavaScript, behaviour, usercontrols

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: JavaScript | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 834 | Hits: 36

Similar Posts

  • ASP.NET 4 Beta 2 - New Version, New Docs, New MSDN Site ! more
  • IDCC conference in Israel – Spet. 14th more
  • AJAX Logging with Exponential Backoff more
  • A Lightweight Event Framework in JavaScript more
  • Scripting ASP.NET MVC Views Stored In The Database more
  • Client Rendering Views with Spark and ASP.NET MVC more
  • Periodically Updating the Screen and Web Page Title with ASP.NET AJAX more
  • Custom JSON Serialization in ASP.NET AJAX more
  • JSON Serializers in .NET - not there yet more
  • Effects and Transitions for Silverlight more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD