Skip to content

Commit 38d482f

Browse files
committed
Added a fallback to using $http if js-data-http isn't loaded.
1 parent 09962e9 commit 38d482f

File tree

5 files changed

+476
-48
lines changed

5 files changed

+476
-48
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Jason Dobry",
33
"name": "js-data-angular",
4-
"description": "Angular wrapper for js-data.",
4+
"description": "Angular wrapper for js-data (originally angular-data).",
55
"version": "2.0.0-alpha.1-0",
66
"homepage": "http://www.js-data.io/js-data-angular",
77
"repository": {
@@ -19,6 +19,6 @@
1919
"package.json"
2020
],
2121
"dependencies": {
22-
"js-data": "~0.4.x"
22+
"js-data": "~1.0.x"
2323
}
2424
}

dist/js-data-angular.js

Lines changed: 235 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
(function (window, angular, undefined) {
1313
'use strict';
1414

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+
1535
var adapters = [
1636
{
1737
project: 'js-data-http',
@@ -35,6 +55,26 @@
3555
}
3656
];
3757

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+
3878
function registerAdapter(adapter) {
3979
var Adapter;
4080

@@ -49,6 +89,9 @@
4989
}
5090

5191
if (Adapter) {
92+
if (adapter.name === 'http') {
93+
httpLoaded = true;
94+
}
5295
adapter.loaded = true;
5396
angular.module(adapter.project, ['ng']).provider(adapter.class, function () {
5497
var _this = this;
@@ -64,32 +107,203 @@
64107
registerAdapter(adapters[i]);
65108
}
66109

67-
var JSData;
110+
if (!httpLoaded) {
111+
var defaultsPrototype = Defaults.prototype;
68112

69-
try {
70-
JSData = require('js-data');
71-
} catch (e) {
113+
defaultsPrototype.queryTransform = function (resourceName, params) {
114+
return params;
115+
};
72116

73-
}
117+
defaultsPrototype.basePath = '';
74118

75-
if (!JSData) {
76-
JSData = window.JSData;
77-
}
119+
defaultsPrototype.forceTrailingSlash = '';
78120

79-
if (!JSData) {
80-
throw new Error('js-data must be loaded!');
81-
}
121+
defaultsPrototype.httpConfig = {};
82122

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+
}
93307

94308
angular.module('js-data', ['ng'])
95309
.value('DSUtils', JSData.DSUtils)

dist/js-data-angular.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
],
2121
"devDependencies": {
2222
"grunt": "0.4.5",
23-
"grunt-browserify": "3.0.1",
23+
"grunt-browserify": "3.1.0",
2424
"grunt-contrib-clean": "0.6.0",
2525
"grunt-contrib-jshint": "0.10.0",
2626
"grunt-contrib-uglify": "0.6.0",
2727
"grunt-contrib-watch": "0.6.1",
2828
"time-grunt": "1.0.0",
29-
"jit-grunt": "0.8.0"
29+
"jit-grunt": "0.9.0"
3030
},
3131
"scripts": {
3232
"test": "grunt test"
3333
},
3434
"dependencies": {
3535
"mout": "0.10.0",
36-
"js-data": "~0.4.x"
36+
"js-data": "~1.0.x"
3737
}
3838
}

0 commit comments

Comments
 (0)