Adding loading service to allow set a spinner on button during request

stable
Jesús Espino 2014-08-29 14:01:11 +02:00
parent dcdd9d8d8f
commit 58923eaa81
2 changed files with 41 additions and 11 deletions

View File

@ -146,7 +146,7 @@ module.directive("tgLbBlock", ["lightboxService", BlockLightboxDirective])
## Create/Edit Userstory Lightbox Directive
#############################################################################
CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService) ->
CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService, $loading) ->
link = ($scope, $el, attrs) ->
isNew = true
@ -202,32 +202,25 @@ CreateEditUserstoryDirective = ($repo, $model, $rs, $rootScope, lightboxService)
form = $el.find("form").checksley()
target = angular.element(event.currentTarget)
loading = "<span class='icon icon-spinner'></span>" #Create spinner item
finish = target.text() #Save current text
if not form.validate()
return
$loading.start(target)
if isNew
target.addClass('loading').html(loading) # Add item
promise = $repo.create("userstories", $scope.us)
broadcastEvent = "usform:new:success"
else
target.addClass('loading').html(loading) # Add item
promise = $repo.save($scope.us)
broadcastEvent = "usform:edit:success"
promise.then (data) ->
target.removeClass('loading').html(finish) # Add item
$loading.finish(target)
lightboxService.close($el)
$rootScope.$broadcast(broadcastEvent, data)
promise.then null, (data) ->
target.removeClass('loading').html(finish) # Add item
$loading.finish(target)
form.setErrors(data)
if data._error_message
$confirm.notify("error", data._error_message)
@ -260,6 +253,7 @@ module.directive("tgLbCreateEditUserstory", [
"$tgResources",
"$rootScope",
"lightboxService",
"loadingService",
CreateEditUserstoryDirective
])

View File

@ -0,0 +1,36 @@
###
# Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2014 Jesús Espino Garcia <jespinog@gmail.com>
# Copyright (C) 2014 David Barragán Merino <bameda@dbarragan.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# File: modules/lightboxes.coffee
###
module = angular.module("taigaCommon")
class LoadingService extends taiga.Service
start: (target) ->
target.data('loading-old-content', target.html())
target.addClass('loading')
target.html("<span class='icon icon-spinner'></span>")
finish: (target) ->
oldContent = target.data('loading-old-content')
target.data('loading-old-content', null)
target.html(oldContent)
target.removeClass('loading')
module.service("loadingService", LoadingService)