Skip to content

Commit 6918e5d

Browse files
author
Keith Aleq Jackson
committed
Add configurations for multi-API Angular apps
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 c8cb9ae commit 6918e5d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/services.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ module.exports = function generateServices(app, options) {
5454
options = extend({
5555
ngModuleName: 'lbServices',
5656
apiUrl: '/',
57+
includeCommonModules: true,
58+
namespaceModels: false,
59+
namespaceDelimiter: '.',
5760
}, options);
5861

5962
var models = describeModels(app, options);
@@ -67,6 +70,9 @@ module.exports = function generateServices(app, options) {
6770
moduleName: options.ngModuleName,
6871
models: models,
6972
urlBase: options.apiUrl.replace(/\/+$/, ''),
73+
includeCommonModules: options.includeCommonModules,
74+
namespaceModels: options.namespaceModels,
75+
namespaceDelimiter: options.namespaceDelimiter,
7076
});
7177
};
7278

lib/services.template.ejs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
3535
*/
3636
var module = angular.module(<%-: moduleName | q %>, ['ngResource']);
3737

38-
<% for (var modelName in models) {
38+
<% function getFormattedModelName(modelName) {
39+
var resourceModelName = modelName[0].toUpperCase() + modelName.slice(1);
40+
if (namespaceModels) {
41+
resourceModelName = moduleName + namespaceDelimiter + resourceModelName;
42+
}
43+
return resourceModelName;
44+
}
45+
for (var modelName in models) {
3946
var meta = models[modelName];
40-
// capitalize the model name
41-
modelName = modelName[0].toUpperCase() + modelName.slice(1);
47+
// capitalize/namespace the model name
48+
modelName = getFormattedModelName(modelName);
4249
-%>
4350
/**
4451
* @ngdoc object
@@ -301,6 +308,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
301308
302309
<% } // for modelName in models -%>
303310
311+
<% if (includeCommonModules) { %>
304312
module
305313
.factory('LoopBackAuth', function() {
306314
var props = ['accessTokenId', 'currentUserId', 'rememberMe'];
@@ -390,7 +398,6 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
390398
},
391399
};
392400
}])
393-
394401
/**
395402
* @ngdoc object
396403
* @name <%-: moduleName %>.LoopBackResourceProvider
@@ -480,6 +487,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
480487
};
481488
}];
482489
});
490+
<% } // end if (includeCommonModules) %>
483491
<%
484492
function getJsDocType(arg) {
485493
var type = arg.type == 'any' ? '*' : arg.type;

0 commit comments

Comments
 (0)