This repository has been archived by the owner on Aug 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasty-resource.coffee
151 lines (104 loc) · 3.78 KB
/
tasty-resource.coffee
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# The MIT License (MIT)
# Copyright (c) 2013 Goran Sterjov
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
root = exports ? this
class root.TastyResourceFactory
constructor: (@$http, @_config)->
@_config.cache ||= false
@_resolved = true
queryURL: (url, parameters, success, error)->
results = []
# construct the filter params
params = []
for attr, value of parameters
params.push "#{attr}=#{value}"
url = "#{url}?#{params.join('&')}" if params.length > 0
@_resolved = false
promise = @$http.get url, cache: @_config.cache
promise.then (response)=>
if response.data.objects?
for object in response.data.objects
results.push @_create_resource(object)
results.meta = response.data.meta
else
angular.copy(response.data, results)
promise.then ()=> @_resolved = true
promise.then success, error
results
query: (filter, success, error)->
@queryURL @_config.url, filter, success, error
get: (id, success, error)->
url = @_get_detail_url id
resource = new TastyResourceFactory(@$http, @_config)
@_resolved = false
promise = @$http.get url, cache: @_config.cache
promise.then (response)=>
for key, value of response.data
resource[key] = value
promise.then ()=> @_resolved = true
promise.then success, error
return resource
post: ()->
@_resolved = false
promise = @$http.post @_config.url, @_get_data()
promise.then ()=> @_resolved = true
promise.success (response, status, headers)=> @_config.detail_url = headers("Location")
return promise
put: (id)->
url = @_get_detail_url id
@_resolved = false
promise = @$http.put url, @_get_data()
promise.then ()=> @_resolved = true
return promise
patch: (id)->
url = @_get_detail_url id
@_resolved = false
promise = @$http method: "PATCH", url: url, data: @_get_data()
promise.then ()=> @_resolved = true
return promise
resolved: ()->
@_resolved
_get_detail_url: (id)->
url = @_config.url
id = @id if not id?
# if id has a leading slash then assume its a resource URI
if id and id[0] is "/"
@_config.detail_url = id
else
@_config.detail_url = "#{@_config.url}#{id}/"
return @_config.detail_url
_get_data: ()->
data = {}
# get the resource data
for attr, value of @
# filter out class features
if typeof value != "function" and attr[0] not in ["$", "_"]
# use the resource uri if the value is another resource
if value instanceof TastyResourceFactory
data[attr] = value.resource_uri
else
data[attr] = value
return data
_create_resource: (data)->
resource = new TastyResourceFactory(@$http, @_config)
for key, value of data
resource[key] = value
return resource
module = angular.module("tastyResource", [])
module.factory "TastyResource", ["$http", ($http)->
(config)->
new TastyResourceFactory($http, config)
]