From 578808ecd39a9dc4cf6bae3bfb3d3b2ffb764e70 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Wed, 11 Sep 2013 16:34:04 -0700 Subject: [PATCH] jquery plugin to attach a promise to a spinning icon --- UI/jQuery/jquery.spin.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/UI/jQuery/jquery.spin.js b/UI/jQuery/jquery.spin.js index c975db6a1..17fe52edf 100644 --- a/UI/jQuery/jquery.spin.js +++ b/UI/jQuery/jquery.spin.js @@ -4,9 +4,22 @@ define( ], function ($) { 'use strict'; + $.fn.spinForPromise = function (promise) { + var self = this; + + if (!promise || promise.state() !== 'pending') { + return this; + } + promise.always(function () { + self.stopSpin(); + }); + + return this.startSpin(); + }; + $.fn.startSpin = function () { - var icon = this.find('i'); + var icon = this.find('i').andSelf('i'); var iconClasses = icon.attr('class').match(/(?:^|\s)icon\-.+?(?:$|\s)/); @@ -31,7 +44,7 @@ define( }; $.fn.stopSpin = function () { - var icon = this.find('i'); + var icon = this.find('i').andSelf('i'); this.removeClass('disabled'); icon.removeClass('icon-spin icon-nd-spinner');