var accordion = new $.ui.accordion({ header: "h3" }, $('#accordion')[0]),
someEl = $('#something')
someEl.bind('click', function (){
// can manage state of the object directly
accordion.activate(2)
})
// A little AOP
var _activate = accordion.activate
accordion.activate = function (index){
// do something every time it's activated
// (I know there's an event for this, just illustrating)
_activate.apply(accordion, arguments)
}
If this kind of API was not only cleaned up but also documented and encouraged:
I think we'd see cleaner jQuery in the wild.
Beginners would become more familiar with the JavaScript language
Instances methods are mutable and can be passed around the app (main reason I do this)
After all, the accordion is a separate object from the element, why enforce the relationship through the bridge?
Though this works as it stands (and is how I use it) there are a couple of problems with it:
It's a constructor but it's not capitalized (would be fine if you don't need `new`)
The options should be the second argument for clearer creation
Widgets should take a selector for the element (just grab the first matched item), or a real DOM element (as it stands now).
That said, I'm not against the bridge, it's a handy shortcut in many cases. However, using the actual widget object is clearer and terser. It also encourages developers to quit running everything through Sizzle every time they want to manipulate a UI instance.