> You could pass the changed element as additional arguments to the event,
> so you just update those, something like this:
> // jquery.myPlugin.js
> $(window).bind('myPluginReload', function(event, changedElement) {
> $("find stuff", changedElement).myPlugin({do: "things"});
> });
> // Content being returned via AJAX
> $(window).trigger('myPluginReload', changedElement);
Here's the plugin I've ended up using:
jquery.behaviors.js:
-------
/**
* Add behaviors to the jQuery object.
*/
jQuery.extend({
behaviors: [],
registerBehavior: function(attachFunction, ready) {
// Default to true.
var ready = (ready == null) ? true : ready;
jQuery.behaviors.push(attachFunction);
if (ready) {
jQuery.readyList.push(attachFunction);
}
}
});
/**
* Attach registered behaviors.
*/
jQuery.fn.attachBehaviors = function() {
var elt = this;
if (jQuery.behaviors) {
// Execute all of them.
jQuery.each(jQuery.behaviors, function(){
this.apply(elt);
});
}
};
-------
It's used like this:
// Register a behavior:
$.registerBehavior(mybehavior.attach);
// Attach all registered behaviors to an element:
$(elt).attachBehaviors();
Comments on the implementation?
I've submitted this as a patch to Drupal, http://drupal.org/node/125153. I'd
also like to contribute it as a jQuery plugin. Consistency in how we do this
will improve interoperability between different jQuery-based AJAX
applications.
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/