jQuery.Behavior: Simple jQuery library for boosting productivity
rodpetrovic.com
rodpetrovic.com
http://docs.jquery.com/Plugins/Authoring
Plus Event Binding:
http://blog.rebeccamurphey.com/2009/12/03/demystifying-custo...
It's here: http://things.rodpetrovic.com/post/283585362/why-jquery-beha...
In short: I'm well aware of custom events but I don't think they provide me with object-oriented encapsulation of functionality.
I know the introduction is a bit long, sorry. I wrote it while I was hacking and I think I was even drinking some wine during that process, so... I'll write a shorter, clearer introduction soon, I promise.
The problems that occur when your JavaScript (jQuery) code starts piling up are nothing new. I've been finding ways to organize my code for ages.
The idea behind behaviors is simple really:
1. Encapsulate some type of functionality within a good old function. 2. "Attach" this function to some elements as their "behavior". 3. Methods and properties of all behaviors can be accessed from anywhere, which allows interaction.
It's a simple object-oriented philosophy and it's nothing new. I just think it would be nice to have a standard way of organizing huge piles of JavaScript without having to use some huge JS framework or moving too far away from the jQuery coding style.
$.fn.misbehave = function () { alert('Oh behave!');}
$('.bad-behavior').misbehave();
?What is gain by adding an extra "object-oriented" wrapper around standard jquery functionality?
Even the simple example: #("#foo").missbehave is quite bad:
missbehave has to be defined as a jQuery global behavior on all objects, not something that is specific to every single dom element to which it was assigned:
$("#foo,#bar,#baz").behavior("missbehave") would mean that each of the 3 have potentially 3 different behaviors for missbehave in the same scheme of things, some might not even have those behaviors.
Think more along the lines of the state of a page:
You set a state, which configures behaviors. Execute the behaviors.
Change a state, change behaviors, execute them again.
Makes state management much easier. God I am wishing I had this about 3 weeks ago when I started my current project :)
-Once you apply a behavior to an element, there is no way to remove it
-There is no way to access the behavior by type, so if you add two different behaviors to an element, you can't (or at least, from what I can glean from the documentation) access one of the specific behaviors. This could get problematic if you have two different behaviors with identically named properties.
Just like events, being able to apply and remove things like this is very helpful.
$('.some-behavior').each(function () { this.behavior = null; });
I'll make sure to add a prettier way of doing this in the next release.
As for the multiple behaviors issue - it's a philosophical question: Should it be possible for one element to be an instance of more than one behavior or should we let the developer "mix" several behaviors into one, which is simple and not really that hard to do? I chose the latter.