|
Behaviors
Introduction to the Behaviors library.
View the presentation from The Ajax Experience. IntroductionOur Behaviors library was originally inspired by Ben Nolan’s Behaviours library. Basically, the idea is to use CSS selectors to “decorate” DOM elements with Javascript handlers and the like. We took the idea a step farther and pulled them out into a separate file, called a behaviors stylesheet (BSS). This is a stylesheet, using the same syntax as CSS, that includes a variety of attributes to support DOM decoration. For example, you can add a mouseover handler to show a preview window for all links marked with the preview class like this: a.preview { mouseover: preview; } You can also include traditional CSS elements, so if you want, you can simply add you BSS attributes to your existing CSS. This is no longer the case. Our implementation of this feature was somewhat flawed; we may re-introduce it in a future release, but for now, it has been removed. You can load an BSS file pretty much the same way you load an CSS file: with a link tag. The benefit of using BSS is two-fold:
Attributesbinding. Binds the element to another element via a binding function. Event handling will be delegated to the bound element. // create a submit button
a.save { binding: up(form); click: submit; }
// tabbed dialog
div.tabs { binding: new( TabControl ) }
a.tab { binding: up( div.tabs ); click: select }Binding functions:
load. Calls a function when the element is loaded (actually at the point the attribute is processed). Passes the bound element to the function. a.menu-item { binding: object( MainMenu ); load: addMenuItem; }Note: With binding and load, order does matter, since both attributes are processed as they are parsed. So if you are depending on the binding for your load method, make sure you put the binding attribute first. blur, change, click, dblclick, focus, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, mouseup, and resize. Specifies the handler for the given event. The handler must be a method of the element. See binding to add methods to an element. a.preview { binding: new( Previewable ); click: preview; }height, width. Sets a given style attribute relative to another element’s value. // make sure the sidebar and body match
div.sidebar { height: minimum( div.body ) }
div.body { height: minimum( div.sidebar ) }Relative value functions:
Note: Relative values only work when the values are independent of one another. Extensibility It is straightforward to add new attributes. In fact, my hope is that people will contribute useful attributes that we can incorporate into the built-in attributes. All you need to do is add a method to the hash Behaviors.Attributes and voila! instant attribute. The function you add should take three arguments: the element that the attribute is operating on, the value of the attribute, and the attribute name itself. (In most cases, you can ignore the third argument.) Behaviors.Attributes.myAttribute = function(el,val) { ... }
// later, inside the BSS file ...
div.foo { my-attribute: bar; }The only rules are:
|
Sign in to add a comment
asdasdasda
asdfasdf