|
12 | 12 | (function (window, angular, undefined) {
|
13 | 13 | 'use strict';
|
14 | 14 |
|
| 15 | + var JSData; |
| 16 | + |
| 17 | + try { |
| 18 | + JSData = require('js-data'); |
| 19 | + } catch (e) { |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + if (!JSData) { |
| 24 | + JSData = window.JSData; |
| 25 | + } |
| 26 | + |
| 27 | + if (!JSData) { |
| 28 | + throw new Error('js-data must be loaded!'); |
| 29 | + } |
| 30 | + |
| 31 | + var makePath = JSData.DSUtils.makePath; |
| 32 | + var deepMixIn = JSData.DSUtils.deepMixIn; |
| 33 | + var httpLoaded = false; |
| 34 | + |
15 | 35 | var adapters = [
|
16 | 36 | {
|
17 | 37 | project: 'js-data-http',
|
|
35 | 55 | }
|
36 | 56 | ];
|
37 | 57 |
|
| 58 | + var functionsToWrap = [ |
| 59 | + 'compute', |
| 60 | + 'digest', |
| 61 | + 'eject', |
| 62 | + 'inject', |
| 63 | + 'link', |
| 64 | + 'linkAll', |
| 65 | + 'linkInverse', |
| 66 | + 'unlinkInverse' |
| 67 | + ]; |
| 68 | + |
| 69 | + function Defaults() { |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + function DSHttpAdapter(options) { |
| 74 | + this.defaults = new Defaults(); |
| 75 | + deepMixIn(this.defaults, options); |
| 76 | + } |
| 77 | + |
38 | 78 | function registerAdapter(adapter) {
|
39 | 79 | var Adapter;
|
40 | 80 |
|
|
49 | 89 | }
|
50 | 90 |
|
51 | 91 | if (Adapter) {
|
| 92 | + if (adapter.name === 'http') { |
| 93 | + httpLoaded = true; |
| 94 | + } |
52 | 95 | adapter.loaded = true;
|
53 | 96 | angular.module(adapter.project, ['ng']).provider(adapter.class, function () {
|
54 | 97 | var _this = this;
|
|
64 | 107 | registerAdapter(adapters[i]);
|
65 | 108 | }
|
66 | 109 |
|
67 |
| - var JSData; |
| 110 | + if (!httpLoaded) { |
| 111 | + var defaultsPrototype = Defaults.prototype; |
68 | 112 |
|
69 |
| - try { |
70 |
| - JSData = require('js-data'); |
71 |
| - } catch (e) { |
| 113 | + defaultsPrototype.queryTransform = function (resourceName, params) { |
| 114 | + return params; |
| 115 | + }; |
72 | 116 |
|
73 |
| - } |
| 117 | + defaultsPrototype.basePath = ''; |
74 | 118 |
|
75 |
| - if (!JSData) { |
76 |
| - JSData = window.JSData; |
77 |
| - } |
| 119 | + defaultsPrototype.forceTrailingSlash = ''; |
78 | 120 |
|
79 |
| - if (!JSData) { |
80 |
| - throw new Error('js-data must be loaded!'); |
81 |
| - } |
| 121 | + defaultsPrototype.httpConfig = {}; |
82 | 122 |
|
83 |
| - var functionsToWrap = [ |
84 |
| - 'compute', |
85 |
| - 'digest', |
86 |
| - 'eject', |
87 |
| - 'inject', |
88 |
| - 'link', |
89 |
| - 'linkAll', |
90 |
| - 'linkInverse', |
91 |
| - 'unlinkInverse' |
92 |
| - ]; |
| 123 | + defaultsPrototype.log = console ? function (a, b, c, d, e) { |
| 124 | + console.log(a, b, c, d, e); |
| 125 | + } : function () { |
| 126 | + }; |
| 127 | + |
| 128 | + defaultsPrototype.deserialize = function (resourceName, data) { |
| 129 | + return data ? ('data' in data ? data.data : data) : data; |
| 130 | + }; |
| 131 | + |
| 132 | + defaultsPrototype.serialize = function (resourceName, data) { |
| 133 | + return data; |
| 134 | + }; |
| 135 | + |
| 136 | + var dsHttpAdapterPrototype = DSHttpAdapter.prototype; |
| 137 | + |
| 138 | + dsHttpAdapterPrototype.getIdPath = function (resourceConfig, options, id) { |
| 139 | + return makePath(options.basePath || this.defaults.basePath || resourceConfig.basePath, resourceConfig.getEndpoint(id, options), id); |
| 140 | + }; |
| 141 | + |
| 142 | + dsHttpAdapterPrototype.getAllPath = function (resourceConfig, options) { |
| 143 | + return makePath(options.basePath || this.defaults.basePath || resourceConfig.basePath, resourceConfig.getEndpoint(null, options)); |
| 144 | + }; |
| 145 | + |
| 146 | + dsHttpAdapterPrototype.GET = function (url, config) { |
| 147 | + config = config || {}; |
| 148 | + if (!('method' in config)) { |
| 149 | + config.method = 'get'; |
| 150 | + } |
| 151 | + return this.HTTP(deepMixIn(config, { |
| 152 | + url: url |
| 153 | + })); |
| 154 | + }; |
| 155 | + |
| 156 | + dsHttpAdapterPrototype.POST = function (url, attrs, config) { |
| 157 | + config = config || {}; |
| 158 | + if (!('method' in config)) { |
| 159 | + config.method = 'post'; |
| 160 | + } |
| 161 | + return this.HTTP(deepMixIn(config, { |
| 162 | + url: url, |
| 163 | + data: attrs |
| 164 | + })); |
| 165 | + }; |
| 166 | + |
| 167 | + dsHttpAdapterPrototype.PUT = function (url, attrs, config) { |
| 168 | + config = config || {}; |
| 169 | + if (!('method' in config)) { |
| 170 | + config.method = 'put'; |
| 171 | + } |
| 172 | + return this.HTTP(deepMixIn(config, { |
| 173 | + url: url, |
| 174 | + data: attrs || {} |
| 175 | + })); |
| 176 | + }; |
| 177 | + |
| 178 | + dsHttpAdapterPrototype.DEL = function (url, config) { |
| 179 | + config = config || {}; |
| 180 | + if (!('method' in config)) { |
| 181 | + config.method = 'delete'; |
| 182 | + } |
| 183 | + return this.HTTP(deepMixIn(config, { |
| 184 | + url: url |
| 185 | + })); |
| 186 | + }; |
| 187 | + |
| 188 | + dsHttpAdapterPrototype.find = function (resourceConfig, id, options) { |
| 189 | + var _this = this; |
| 190 | + options = options || {}; |
| 191 | + return _this.GET( |
| 192 | + _this.getIdPath(resourceConfig, options, id), |
| 193 | + options |
| 194 | + ).then(function (data) { |
| 195 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 196 | + }); |
| 197 | + }; |
| 198 | + |
| 199 | + dsHttpAdapterPrototype.findAll = function (resourceConfig, params, options) { |
| 200 | + var _this = this; |
| 201 | + options = options || {}; |
| 202 | + options.params = options.params || {}; |
| 203 | + if (params) { |
| 204 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
| 205 | + deepMixIn(options.params, params); |
| 206 | + } |
| 207 | + return _this.GET( |
| 208 | + _this.getAllPath(resourceConfig, options), |
| 209 | + options |
| 210 | + ).then(function (data) { |
| 211 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 212 | + }); |
| 213 | + }; |
| 214 | + |
| 215 | + dsHttpAdapterPrototype.create = function (resourceConfig, attrs, options) { |
| 216 | + var _this = this; |
| 217 | + options = options || {}; |
| 218 | + return _this.POST( |
| 219 | + makePath(options.basePath || this.defaults.basePath || resourceConfig.basePath, resourceConfig.getEndpoint(attrs, options)), |
| 220 | + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
| 221 | + options |
| 222 | + ).then(function (data) { |
| 223 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 224 | + }); |
| 225 | + }; |
| 226 | + |
| 227 | + dsHttpAdapterPrototype.update = function (resourceConfig, id, attrs, options) { |
| 228 | + var _this = this; |
| 229 | + options = options || {}; |
| 230 | + return _this.PUT( |
| 231 | + _this.getIdPath(resourceConfig, options, id), |
| 232 | + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
| 233 | + options |
| 234 | + ).then(function (data) { |
| 235 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 236 | + }); |
| 237 | + }; |
| 238 | + |
| 239 | + dsHttpAdapterPrototype.updateAll = function (resourceConfig, attrs, params, options) { |
| 240 | + var _this = this; |
| 241 | + options = options || {}; |
| 242 | + options.params = options.params || {}; |
| 243 | + if (params) { |
| 244 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
| 245 | + deepMixIn(options.params, params); |
| 246 | + } |
| 247 | + return this.PUT( |
| 248 | + _this.getAllPath(resourceConfig, options), |
| 249 | + options.serialize ? options.serialize(resourceConfig.name, attrs) : _this.defaults.serialize(resourceConfig.name, attrs), |
| 250 | + options |
| 251 | + ).then(function (data) { |
| 252 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 253 | + }); |
| 254 | + }; |
| 255 | + |
| 256 | + dsHttpAdapterPrototype.destroy = function (resourceConfig, id, options) { |
| 257 | + var _this = this; |
| 258 | + options = options || {}; |
| 259 | + return _this.DEL( |
| 260 | + _this.getIdPath(resourceConfig, options, id), |
| 261 | + options |
| 262 | + ).then(function (data) { |
| 263 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 264 | + }); |
| 265 | + }; |
| 266 | + |
| 267 | + dsHttpAdapterPrototype.destroyAll = function (resourceConfig, params, options) { |
| 268 | + var _this = this; |
| 269 | + options = options || {}; |
| 270 | + options.params = options.params || {}; |
| 271 | + if (params) { |
| 272 | + params = _this.defaults.queryTransform(resourceConfig.name, params); |
| 273 | + deepMixIn(options.params, params); |
| 274 | + } |
| 275 | + return this.DEL( |
| 276 | + _this.getAllPath(resourceConfig, options), |
| 277 | + options |
| 278 | + ).then(function (data) { |
| 279 | + return (options.deserialize ? options.deserialize : _this.defaults.deserialize)(resourceConfig.name, data); |
| 280 | + }); |
| 281 | + }; |
| 282 | + |
| 283 | + angular.module('js-data').provider('DSHttpAdapter', function () { |
| 284 | + var _this = this; |
| 285 | + _this.defaults = {}; |
| 286 | + _this.$get = ['$http', function ($http) { |
| 287 | + dsHttpAdapterPrototype.HTTP = function (config) { |
| 288 | + var _this = this; |
| 289 | + var start = new Date().getTime(); |
| 290 | + config = deepMixIn(config, _this.defaults.httpConfig); |
| 291 | + if (_this.defaults.forceTrailingSlash && config.url[config.url.length] !== '/') { |
| 292 | + config.url += '/'; |
| 293 | + } |
| 294 | + config.method = config.method.toUpperCase(); |
| 295 | + return $http(config).then(function (data) { |
| 296 | + if (_this.defaults.log) { |
| 297 | + _this.defaults.log(data.config.method.toUpperCase() + ' request: ' + data.config.url + ' Time taken: ' + (new Date().getTime() - start) + 'ms', data); |
| 298 | + } |
| 299 | + return data; |
| 300 | + }); |
| 301 | + }; |
| 302 | + |
| 303 | + return new DSHttpAdapter(_this.defaults); |
| 304 | + }]; |
| 305 | + }); |
| 306 | + } |
93 | 307 |
|
94 | 308 | angular.module('js-data', ['ng'])
|
95 | 309 | .value('DSUtils', JSData.DSUtils)
|
|
0 commit comments