NADAV: How do behaviors compare with Netscape's Action Sheets proposal? Are they just two ways of attacking the same problem(s)?
MICHAEL: If you compare the capabilities of Action Sheets to the behaviors' goal of creating a component model, then you'll see that Action Sheets have some crucial missing bits. Action Sheets allow a developer to create an encapsulation of event handlers. For example, Action Sheets can trivially solve the mouse-over problem. I'm always writing the onmouseover/onmouseout event handlers for elements and changing their color/size/whatever. With Action Sheets, instead of setting two event handlers, I can do the same thing and use less code.
Another example of something more easily done with behaviors is the "type
masking" entry field. [Ed: "type masking" refers to the population of a
form field with symbols, like parentheses or dashes, to help users
understand what format they should use to enter, say, their phone or social
security numbers.] This could also be done with Action Sheets as the essence of the behavior is catching the onfocus/onblur event of an entry field and formatting the content. Our mask behavior, however, has one big advantage - encapsulation (which makes components more stable, because it means that people can call only the properties and methods that you allow them to call).
To do masking, you not only need to have event handlers for the onfocus/onblur events, but you need functions to actually do the formatting and variables to keep state. In our mask example, we have the following methods:
function doFocus()
function doBlur()
function format()
function formatTime()
function formatMoney()
function formatDate()
If you want to selectively expose methods, properties, and events to the outside world, behaviors allow you to do so. There is declarative syntax at the top of the behavior definition that controls the exposure of the methods/properties and events in your behavior. This declarative syntax is also important for tools that would like to support behaviors. It's important for a tool to be able to read the "signature" of a component quickly, and behaviors let you do that.
The mask behavior does declare a public variable "maskType." This allows the developer to have one mask behavior for all the different types of masking you might want to do - like money, time, date, social security #, phone #, etc.
With Action Sheets, there is no ability to declare properties. Well, expandos might be enough, but there are problems there. Expandos have weird case sensitivity rules when declared from HTML, which makes it really hard for tools to support them and for users to fill out those properties.
Bottom line: Behaviors deliver a component model that provides a complete event model, encapsulation, public/private control, and a true binary extensibility model. Action Sheets really only have an event model, and even that's lacking given that events can only be captured for the element with the action applied.
next page»