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.
I think the idea is to select multiple objects, assign them whatever behaviors you wish (if they exist) and execute those behaviors.
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.
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.
I think in most cases you can get away with using a single behavior for a single control that encapsulates all of the control's logic. I suppose if you only have one: removing a behavior is as simple as just re-creating the element. A bit clunky, so an API for this would be nice, but it works.
nice. I don't 100% understand it or how you would use it and why you would use it and not just write jquery code and structure it logically,... but then again I'm constantly finding my jquery code to slowly becoming big and using global variables and stuff to encapsulte objects' behavior... but unfortunately, I don't really understand this enough...
http://docs.jquery.com/Plugins/Authoring
Plus Event Binding:
http://blog.rebeccamurphey.com/2009/12/03/demystifying-custo...