I have created a jQuery plugin that allows the user to interact with a tree (creating, updating, deleting nodes). There are at least a dozen methods for interacting with the tree. Ideally, I don’t want to pollute the jQuery namespace with all of these tree-specific methods as I am doing now as each methods presents an additional opportunity for a namespace collision. I just want the one method for activating the plugin itself and so I am looking to refactor.
$('ol#tree').tree(options) // just a single jQuery method for my plugin is fine…
$('li#item4').moveNode('li#item6') // …however, we also have this supporting method
$('ol#tree').appendNode(node) // …and this one
$('li#item2').expandBranch() // …and this one, etc.
What are some practices for exposing an interface to the activated DOM element and its sub-elements that don’t pollute the jQuery namespace?
I have toyed with saving jQuery object that comes back when I create the tree.
var tree = $('ol#tree').tree();
This tree object itself would be extended to include the methods. Unfortunately, as the tree itself is being regularly manipulated, this runs the risk of having the state of that object fall out of sync with the state of the parent DOM element and its sub-elements. I’m not sure trying to maintain state in that object is worth it, since the DOM itself will always have the current state.
I have also considered using a namespace (via John Resig’s Space plugin):
$('ol#tree').tree()
$('ol#tree').tree.appendNode(node)
$('ol#tree').tree.item('li#item2').expandBranch()
How do you handle this problem? Could you point me to some existing plugins that address this issue from whose code I could learn?