cancelable search

stable
Juanfran 2015-12-11 10:03:42 +01:00 committed by David Barragán Merino
parent 31224ecbd5
commit 26006f2940
5 changed files with 36 additions and 19 deletions

View File

@ -419,7 +419,7 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven
# Add next param when user try to access to a secction need auth permissions. # Add next param when user try to access to a secction need auth permissions.
authHttpIntercept = ($q, $location, $navUrls, $lightboxService) -> authHttpIntercept = ($q, $location, $navUrls, $lightboxService) ->
httpResponseError = (response) -> httpResponseError = (response) ->
if response.status == 0 || response.status == -1 if response.status == 0 || (response.status == -1 && !response.config.cancelable)
$lightboxService.closeAll() $lightboxService.closeAll()
$location.path($navUrls.resolve("error")) $location.path($navUrls.resolve("error"))
$location.replace() $location.replace()

View File

@ -45,8 +45,6 @@ class HttpService extends taiga.Service
request: (options) -> request: (options) ->
options.headers = _.merge({}, options.headers or {}, @.headers()) options.headers = _.merge({}, options.headers or {}, @.headers())
if _.isPlainObject(options.data)
options.data = JSON.stringify(options.data)
return @http(options) return @http(options)

View File

@ -69,6 +69,8 @@ MarkitupDirective = ($rootscope, $rs, $selectedText, $template, $compile, $trans
$scope.$on "markdown-editor:submit", -> $scope.$on "markdown-editor:submit", ->
closePreviewMode() closePreviewMode()
cancelablePromise = null
preview = -> preview = ->
markdownDomNode = element.parents(".markdown") markdownDomNode = element.parents(".markdown")
markItUpDomNode = element.parents(".markItUp") markItUpDomNode = element.parents(".markItUp")
@ -370,7 +372,11 @@ MarkitupDirective = ($rootscope, $rs, $selectedText, $template, $compile, $trans
return true return true
return false return false
$rs.search.do($scope.projectId, term).then (res) => cancelablePromise.abort() if cancelablePromise
cancelablePromise = $rs.search.do($scope.projectId, term)
cancelablePromise.then (res) =>
# ignore wikipages if they're the only results. can't exclude them in search # ignore wikipages if they're the only results. can't exclude them in search
if res.count < 1 or res.count == res.wikipages.length if res.count < 1 or res.count == res.wikipages.length
callback([]) callback([])

View File

@ -22,22 +22,39 @@
taiga = @.taiga taiga = @.taiga
resourceProvider = ($repo, $urls, $http) -> resourceProvider = ($repo, $urls, $http, $q) ->
service = {} service = {}
service.do = (projectId, term) -> service.do = (projectId, term) ->
deferredAbort = $q.defer()
url = $urls.resolve("search") url = $urls.resolve("search")
params = { params = {
project: projectId url: url,
text: term, method: "GET",
get_all: false timeout: deferredAbort.promise,
cancelable: true,
params: {
project: projectId
text: term,
get_all: false,
}
} }
return $http.get(url, params).then (data) -> request = $http.request(params).then (data) ->
return data.data return data.data
request.abort = () ->
deferredAbort.resolve()
request.finally = () ->
request.abort = angular.noop
deferredAbort = request = null
return request
return (instance) -> return (instance) ->
instance.search = service instance.search = service
module = angular.module("taigaResources") module = angular.module("taigaResources")
module.factory("$tgSearchResourcesProvider", ["$tgRepo", "$tgUrls", "$tgHttp", resourceProvider]) module.factory("$tgSearchResourcesProvider", ["$tgRepo", "$tgUrls", "$tgHttp", "$q", resourceProvider])

View File

@ -95,19 +95,15 @@ class SearchController extends mixOf(taiga.Controller, taiga.PageMixin)
@scope.loading = true @scope.loading = true
@._loadSearchData(term).then (data) => @._loadSearchData(term).then (data) =>
if data @scope.searchResults = data
@scope.searchResults = data @scope.loading = false
@scope.loading = false
_loadSearchData: (term = "") -> _loadSearchData: (term = "") ->
@.deferredAbort.resolve() if @.deferredAbort @._promise.abort() if @._promise
@.deferredAbort = @q.defer() @._promise = @rs.search.do(@scope.projectId, term)
@rs.search.do(@scope.projectId, term).then (data) => return @._promise
@.deferredAbort.resolve(data)
return @.deferredAbort.promise
loadInitialData: -> loadInitialData: ->
return @.loadProject().then (project) => return @.loadProject().then (project) =>