Re-apply Behavior/Delegator when DOM updates (after Request)

29 views
Skip to first unread message

Rolf-nl

unread,
Mar 25, 2012, 6:48:11 PM3/25/12
to clien...@googlegroups.com
I have Behavior setup in domready pretty much standard like:
var b = new Behavior();
b.passMethods({
  //nothing now
});
b.apply(document.body);
var d = new Delegator({
  getBehavior: function(){ return b; }
}).attach(document.body);

On some click events (using Delegator) I request (JSONP) additional data. With the data that is returned I create new links that are injected into the dom, and I also want to add delegator triggers for various things. How do I re-init Behavior/Delegator correctly to get this working. If I just do new Behavior().apply(document.body); it works, but doesn't look right.

The docs state: [..] It will only invoke a filter once, so it's safe to run it more than once (if the DOM changes, for example).
But should I run Delegator too, is there a better way? TIA

Aaron Newton

unread,
Mar 25, 2012, 11:50:27 PM3/25/12
to clien...@googlegroups.com
Delegator attaches to the document.body and looks for events that bubble up (like click for example). It doesn't need re-application.

Behavior flags elements that have been instantiated so when it encounters them again it skips them (unless you force it).

In theory, you should be able to run b.apply(document.body) a dozen times without ill-effect. When creating new DOM nodes (via Ajax or otherwise) it's better to only do it to the container you are injecting it in instead of the document.body as it does have to check, but that check isn't super expensive. If you look at the examples on dev.clientcide.com of the Delegator.Ajax triggers you'll see how it fetches new data and runs the new elements through behavior. Don't forget to cleanup any elements you destroy.


--
You received this message because you are subscribed to the Google Groups "Clientcide" group.
To view this discussion on the web visit https://groups.google.com/d/msg/clientside/-/2TclAowLv-cJ.
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.

Rolf-nl

unread,
Mar 26, 2012, 6:30:49 AM3/26/12
to clien...@googlegroups.com
Thanks. I haved looked at the Delegator source more than a few times, I'm not sure why I asked about re-applying Delegator as it is "event delegation" that I attach to the body.. hahaha

I will check the examples at the clientcide dev site for Behavior/ajax/etc.
To unsubscribe from this group, send email to clientside+unsubscribe@​googlegroups.com.
Reply all
Reply to author
Forward
0 new messages