While developing with KendoUI we have found kendo.ui.progress(container, toggle) function to be very useful. It's used to show or hide a progress indicator in the container element.
kendo.ui.progress(container, toggle)
At the same time we have found that we usually used it in a context of async operation. This way, we want to show progress, perform some asynchronous operations, hide progress. So, we clearly want to benifit from RAII pattern: we would like to open a progress scope, and to perform some activity withing this scope.
Arguing like this, we have defined a utility function, which is the fusion of kendo.ui.progress() and $.when(). Its signature is like this:
kendo.ui.progress()
$.when()
nesterovskyBros.progress = function(instance /*, task ... */)
where instance is either Model, Widget, JQuery or DOM Element, and task is one or more deferred objects. This function shows a progress and returns a Promise that will hide a progress when all tasks will be complete. Implementation is trivial, so we quote it here:
instance
Model
Widget
JQuery
Element
task
Promise
// Fusion of kendo.ui.progress() and $.when(). scope.progress = function(instance /*, task ... */) { if (instance instanceof Model) { instance = instance.owner && instance.owner(); } if (instance instanceof Widget) { instance = instance.element; } if (instance && instance.nodeType) { instance = $(instance); } var id = ns + "-progress"; // "nesterovskyBros-progress"; var progress = (instance && instance.data(id)) || 0; if (arguments.length < 2) { return progress; } var result = $.when.apply(null, [].slice.call(arguments, 1)); if (instance) { instance.data(id, ++progress); kendo.ui.progress(instance, progress > 0); result.always( function() { progress = instance.data(id) || 0; instance.data(id, --progress); kendo.ui.progress(instance, progress > 0); }); } return result; };
The use is like this:
nesterovskyBros.progress(element, $.ajax("/service1"), $.ajax("/service2")).then(myFunc);
The code can be found at controls.js.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u