summaryrefslogtreecommitdiffstats
path: root/coffee/lib/services/request.coffee
blob: b3a0cf7a5bfa2b86f362a77eec3f4692b90158c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
###
# ownCloud
#
# @author Bernhard Posselt
# Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
#
# This file is licensed under the Affero General Public License version 3 or
# later.
#
# See the COPYING-README file
#
###

angular.module('OC').factory '_Request', ->

	class Request

		constructor: (@_$http, @_$rootScope, @_publisher, @_token, @_router) ->
			@_initialized = false
			@_shelvedRequests = []

			@_$rootScope.$on 'routesLoaded', =>
				@_executeShelvedRequests()
				@_initialized = true
				@_shelvedRequests = []


		request: (route, routeParams={}, data={}, onSuccess=null, onFailure=null, config={}) ->
			# if routes are not ready yet, save the request
			if not @_initialized
				@_shelveRequest(route, routeParams, data, method, config)
				return

			url = @_router.generate(route, routeParams)

			defaultConfig = 
				method: 'GET'
				url: url
				data: data

			# overwrite default values from passed in config
			for key, value of config
				defaultConfig[key] = value

			@_$http(config)
				.success (data, status, headers, config) =>
					if onSuccess
						onSuccess(data, status, headers, config)

					# publish data to models
					for name, value of data.data
						@publisher.publishDataTo(name, value)

				.error (data, status, headers, config) ->
					if onFailure
						onFailure(data, status, headers, config)


		_shelveRequest: (route, routeParams, data, method, config) ->
			request =
				route: route
				routeParams: routeParams
				data: data
				config: config
				method: method

			@_shelvedRequests.push(request)


		_executeShelvedRequests: ->
			for req in @_shelvedRequests
				@post(req.route, req.routeParams, req.data, req.method, req.config)



	return Request