jQuery plugin, function that operates on each jQuery object then executes final statement
Is it possible to write a jQuery plug-in that operates on each item in a jQuery object using this syntax:
(function ($){
$.fn.extend({
DoSomething: function(){
return this.each(function () {
//Do something to each item
});
//Run the general update
update();
}
});
})(jQuery);
But then I want to finish the function call with a general "update" operation.
How would I go about doing something like this? it will be quite slow if I do the update af开发者_JS百科ter each operation, rather than at the end, but I'll be putting this code in a few places, so I was hoping not to separate this out into two calls: $(object).DoSomething().update();
Might not be what you are after, but:
(function ($){
$.fn.extend({
DoSomething: function(){
this.each(function () {
//Do something to each item
});
//Run the general update
update();
return this;
}
});
})(jQuery);
Will execute your this.each()
functionality and then your update() function.
Would something like this work?
$.fn.extend({
doSomething: function (eachProcess, whenDone) {
var result = this.each(eachProcess);
whenDone();
return result;
}
});
So you'd pass in the function to call for each item (eachProcess
) and for completion (whenDone
).
精彩评论