Skip to content

Commit 9fab292

Browse files
author
Keith Aleq Jackson
committed
Add option to leave out definitions for common modules
When connecting to several LoopBack APIs from an Angular app, multiple definitions of LoopBackAuth and LoopBackResource will 'step on' each other, resulting in unpredictable behavior. This change keeps the default behavior consistent, but will omit the module block that generates the LoopBackAuth, LoopBackAuthRequestInterceptor, and LoopBackResource components if a fourth truthy parameter (excludeCommonModules) is passed to generateServices. This will enable loopback-sdk-angular to more seamlessly be used with multiple-API architectures, without having to manually delete sections sections of code generated by the SDK.
1 parent 8437409 commit 9fab292

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/services.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ ejs.filters.q = function(obj) {
2424
* @returns {string} The generated javascript code.
2525
* @header generateServices
2626
*/
27-
module.exports = function generateServices(app, ngModuleName, apiUrl) {
27+
module.exports = function generateServices(
28+
app,
29+
ngModuleName,
30+
apiUrl,
31+
excludeCommonModules
32+
) {
2833
ngModuleName = ngModuleName || 'lbServices';
2934
apiUrl = apiUrl || '/';
3035

@@ -35,10 +40,23 @@ module.exports = function generateServices(app, ngModuleName, apiUrl) {
3540
{ encoding: 'utf-8' }
3641
);
3742

43+
var defineLoopBackAuth = true;
44+
var defineLoopBackAuthRequestInterceptor = true;
45+
var defineLoopBackResource = true;
46+
47+
if (excludeCommonModules === "true") {
48+
defineLoopBackAuth = false;
49+
defineLoopBackAuthRequestInterceptor = false;
50+
defineLoopBackResource = false;
51+
}
52+
3853
return ejs.render(servicesTemplate, {
3954
moduleName: ngModuleName,
4055
models: models,
4156
urlBase: apiUrl.replace(/\/+$/, ''),
57+
defineLoopBackAuth: defineLoopBackAuth,
58+
defineLoopBackAuthRequestInterceptor: defineLoopBackAuthRequestInterceptor,
59+
defineLoopBackResource: defineLoopBackResource
4260
});
4361
};
4462

lib/services.template.ejs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
285285
286286
<% } // for modelName in models -%>
287287
288+
<% if (defineLoopBackAuth || defineLoopBackResource || defineLoopBackAuthRequestInterceptor) { %>
288289
module
290+
<% } %>
291+
292+
<% if (defineLoopBackAuth) { %>
289293
.factory('LoopBackAuth', function() {
290294
var props = ['accessTokenId', 'currentUserId', 'rememberMe'];
291295
var propsPrefix = '$LoopBack$';
@@ -344,6 +348,8 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
344348
return localStorage[key] || sessionStorage[key] || null;
345349
}
346350
})
351+
<% } // end if (defineLoopBackAuth) %>
352+
<% if (defineLoopBackAuthRequestInterceptor) { %>
347353
.config(['$httpProvider', function($httpProvider) {
348354
$httpProvider.interceptors.push('LoopBackAuthRequestInterceptor');
349355
}])
@@ -374,7 +380,9 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
374380
},
375381
};
376382
}])
377-
383+
<% } // end if (defineLoopBackAuthRequestInterceptor) %>
384+
defineLoopBackResource: <%= defineLoopBackResource %>
385+
<% if (defineLoopBackResource) { %>
378386
/**
379387
* @ngdoc object
380388
* @name <%-: moduleName %>.LoopBackResourceProvider
@@ -464,6 +472,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
464472
};
465473
}];
466474
});
475+
<% } // end if (defineLoopBackResource) %>
467476
<%
468477
function getJsDocType(arg) {
469478
var type = arg.type == 'any' ? '*' : arg.type;

0 commit comments

Comments
 (0)