Help with a custom behavior

6 views
Skip to first unread message

Daniel Antonio Peraza Cedrez

unread,
Dec 11, 2011, 12:54:32 AM12/11/11
to clien...@googlegroups.com
I'm trying to implement this custom behavior http://jsfiddle.net/FxEDf/ but I'm in troubles to bind the callback function to the source element.

It is supposed to work like this:

1) A form element (like a checkbox) fires an event.
2) The callback function is then executed upon that event. If it returns TRUE, the children of a container div are injected back into the DOM, otherwise they're removed.

The html should look like this:

<input type="checkbox" data-behavior="ConditionalField" data-ConditionalField-event="change" data-ConditionalField-target="target_elem" data-ConditionalField-callback="function(){ return TRUE};" />

My problem is that api.get('callback') returns the function as string, and passing that string to eval() doesn't seem to achieve the desire casting to Function.

Aaron Newton

unread,
Dec 11, 2011, 1:23:02 AM12/11/11
to clien...@googlegroups.com
behaviors generally aren't intended to accept functions as arguments or options. the whole point of behavior/delegator is to move your JS OUT of the document and into JS files, passing *configuration* along, not functionality. If you pass functions through the behavior options, you're writing JS inline in the HTML, which isn't much better than writing onclick="function(){...}"

Instead, you should move functions into behaviors and delegators.

Delegators are designed to interprete *actions*. Behavior is designed to initialize things. Some behaviors have event handlers (form validators, tabs, etc), but when you have a component that, when interacted with, is intended to facilitate some outcome, consider using a delegator. The deletagor should have all the functionality and negate the need to pass functions to it.

--
You received this message because you are subscribed to the Google Groups "Clientcide" group.
To post to this group, send email to clien...@googlegroups.com.
To unsubscribe from this group, send email to clientside+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clientside?hl=en.

Reply all
Reply to author
Forward
0 new messages