I want to add a callback to a jquery widget.
As an example, I see that the callback in the draggable widgets is enclosed in the following:
$.ui.plugin.add("draggable", "connectToSortable", { // callbacks in here })
Does this mean that I should also wrap my callbacks in this $ .ui.plugin.add ({}); Little? Is there any other way to do this? For example, can I have a function in the widget parameters that could handle this, so that calling my grid would look vaguely:
var foo = { LoadIt: function(url, formid){ var bar = '', $('#baz').grid({ title: {somevar : true}, rowcontent: {data: setup and populate rows}, onComplete: function(){
In my case, I use a grid. The grid loads some json data through an ajax call, and then, now when the dom is filled with the grid, I want to do some manipulation with it (for example, add a background color on hover). Therefore, I imagine the ability to call as part of a grid:
onComplete : function(){
Any tips or suggestions on how to add a callback to a jquery widget?
The example I found confuses me:
var Green5 = { setLevel: function(x){ //... this.element.css({background: greenlevels[level]}); var callback = this.options.change; if ($.isFunction(callback)) callback(level); }, // ... rest of widget definition }; $.widget("ui.green5", Green5); $('.target').green5({change: function(x) { alert ("The color changed to "+x); } });
This was discovered on a site explaining how to add a callback to a jquery widget, but I canβt see anything about the $.ui.plugin.add bit, and I donβt see how change goes into setLevel . How to setLevel get a function that is in change ? If it is simple that everything passed to green5 is an option and therefore accessible through this.options, then where does the callback method come from, calling level in if ($.isFunction(callback)) callback(level); ? I am very puzzled .:(