Skip to content

Commit 751a801

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 627e058 commit 751a801

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
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,9 +40,22 @@ 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,
56+
defineLoopBackAuth: defineLoopBackAuth,
57+
defineLoopBackAuthRequestInterceptor: defineLoopBackAuthRequestInterceptor,
58+
defineLoopBackResource: defineLoopBackResource,
4159
urlBase: apiUrl.replace(/\/+$/, '')
4260
});
4361
};

lib/services.template.ejs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ module.factory(
280280
}]);
281281
282282
<% } // for modelName in models -%>
283-
284283
module
284+
<% if (defineLoopBackAuth) { %>
285285
.factory('LoopBackAuth', function() {
286286
var props = ['accessTokenId', 'currentUserId', 'rememberMe'];
287287
var propsPrefix = '$LoopBack$';
@@ -340,6 +340,8 @@ module
340340
return localStorage[key] || sessionStorage[key] || null;
341341
}
342342
})
343+
<% } // end if (defineLoopBackAuth) %>
344+
<% if (defineLoopBackAuthRequestInterceptor) { %>
343345
.config(['$httpProvider', function($httpProvider) {
344346
$httpProvider.interceptors.push('LoopBackAuthRequestInterceptor');
345347
}])
@@ -371,7 +373,9 @@ module
371373
}
372374
}
373375
}])
374-
376+
<% } // end if (defineLoopBackAuthRequestInterceptor) %>
377+
defineLoopBackResource: <%= defineLoopBackResource %>
378+
<% if (defineLoopBackResource) { %>
375379
/**
376380
* @ngdoc object
377381
* @name <%-: moduleName %>.LoopBackResourceProvider
@@ -461,6 +465,7 @@ module
461465
};
462466
}];
463467
});
468+
<% } // end if (defineLoopBackResource) %>
464469
<%
465470
function getJsDocType(arg) {
466471
var type = arg.type == 'any' ? '*' : arg.type;

0 commit comments

Comments
 (0)