From dfcd54408a3f5a8f39544b7e7b5322e2d9706756 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 26 Sep 2014 14:33:41 +0200 Subject: [PATCH 01/11] Enable client model configuration and extension * Expects loopback style model-config.json as fourth parameter * Expects `dataSource = "remote"` for REST-exposed models * No backward compatibility break if model-config parameter missing * If model-config present, any model not in the file will be skipped * If model-config present, any model not REST-exposed will be skipped * Look for model.js extension files in `modelConfig._meta.sources` * Expects model.js files to be nodejs modules returning same object as ones expected in the loopback boot process Note : `fixture/model-config.json` contains all models used in the test suite so nothing will break. Only MyModel contains custom logic. --- karma.conf.js | 2 +- lib/services.js | 69 ++++++++++++++++++++++- lib/services.template.ejs | 17 ++++++ test.e2e/spec/fixtures/model-config.json | 31 ++++++++++ test.e2e/spec/fixtures/models/my-model.js | 64 +++++++++++++++++++++ test.e2e/spec/services.spec.js | 47 ++++++++++++++- test.e2e/test-server.js | 9 ++- 7 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 test.e2e/spec/fixtures/model-config.json create mode 100644 test.e2e/spec/fixtures/models/my-model.js diff --git a/karma.conf.js b/karma.conf.js index a762168..14356b6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -24,7 +24,7 @@ module.exports = function(config) { // list of files to exclude exclude: [ - // 'test/**/*.js' + 'test.e2e/spec/fixtures/**/*.js' ], diff --git a/lib/services.js b/lib/services.js index 2f4fa96..0a3e8f2 100644 --- a/lib/services.js +++ b/lib/services.js @@ -1,4 +1,5 @@ var fs = require('fs'); +var path = require('path'); var ejs = require('ejs'); ejs.filters.q = function(obj) { @@ -24,12 +25,15 @@ ejs.filters.q = function(obj) { * @returns {string} The generated javascript code. * @header generateServices */ -module.exports = function generateServices(app, ngModuleName, apiUrl) { +module.exports = function generateServices(app, ngModuleName, + apiUrl, clientModelConfig) { ngModuleName = ngModuleName || 'lbServices'; apiUrl = apiUrl || '/'; var models = describeModels(app); + if (!!clientModelConfig) applyClientModelConfig(models, clientModelConfig); + var servicesTemplate = fs.readFileSync( require.resolve('./services.template.ejs'), { encoding: 'utf-8' } @@ -175,3 +179,66 @@ function findModelByName(models, name) { return models[n]; } } + +// Apply client model config, usually from client/model-config.json +function applyClientModelConfig(models, clientModelConfig) { + var modelConfig; + + try { + // Do not use require() below : require caches value, and if it is modified + // next call will use this modified data. Unless object is deep cloned, + // which is an alternative as reading file each time. + var configFile = fs.readFileSync(clientModelConfig, {encoding: 'utf-8'}); + modelConfig = JSON.parse(configFile); + } catch(err) { + console.trace('FATAL: Provided client config file could not be loaded : ' + + clientModelConfig, err); + process.exit(1); + } + + // Default location in loopback workspace, relative to /client/model-config.js + var sources = [ + '../common/models', + './models' + ]; + if (!!modelConfig._meta && Array.isArray(modelConfig._meta.sources)) { + sources = modelConfig._meta.sources; + delete modelConfig._meta; + } + sources = sources.map(function(source) { + return path.join(path.dirname(clientModelConfig), source); + }); + + var n; + // Discard models not present in config + for (n in models) { + if (!(n in modelConfig)) { + console.log('Info: Model ' + n + ' was not defined in client config, ' + + 'it will not be included in generated services file'); + delete models[n]; + } + } + + for (n in modelConfig) { + if (!(n in models) && modelConfig[n].dataSource == 'remote') { + console.log('Warning: Model ' + n + ' is expected to be REST exposed ' + + ' but is not found in app, so it will not be included ' + + ' in generated services file'); + delete modelConfig[n]; + continue; + } + + var meta = models[n]; + meta.extensions = []; + // Supported file syntax for model ModelName : model-name.js + var fsModelName = n.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); + + // Add this model extension functions to models description + // in `extensions` array property + for (var s in sources) { + var file = path.join(sources[s], fsModelName + '.js'); + if (fs.existsSync(file)) + meta.extensions.push(require(file)); + } + } +} diff --git a/lib/services.template.ejs b/lib/services.template.ejs index 87d4495..4ac8aac 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -245,6 +245,23 @@ module.factory( <% }); // forEach methods name -%> <% } // for each scope -%> +<% // Get available model extentions for this model and apply on resource object + meta.extensions.forEach(function(extension) { -%> + + (<%-: extension %>)(R); + +<% }); // forEach model extension -%> + + /** + * @ngdoc property + * @name <%- moduleName %>.<%- modelName %>#modelName + * @propertyOf <%- moduleName %>.<%- modelName %> + * @description + * The name of the model represented by this $resource, + * i.e. `<%- modelName %>`. + */ + R.modelName = <%-: modelName | q %>;* + return R; }]); diff --git a/test.e2e/spec/fixtures/model-config.json b/test.e2e/spec/fixtures/model-config.json new file mode 100644 index 0000000..34b06ed --- /dev/null +++ b/test.e2e/spec/fixtures/model-config.json @@ -0,0 +1,31 @@ +{ + "_meta": { + "sources": [ + "./models" + ] + }, + "MyModel": { + "dataSource": "remote" + }, + "lower-case-not-an-identifier": { + "dataSource": "remote" + }, + "Customer": { + "dataSource": "remote" + }, + "User": { + "dataSource": "remote" + }, + "Product": { + "dataSource": "remote" + }, + "Category": { + "dataSource": "remote" + }, + "Town": { + "dataSource": "remote" + }, + "Country": { + "dataSource": "remote" + } +} diff --git a/test.e2e/spec/fixtures/models/my-model.js b/test.e2e/spec/fixtures/models/my-model.js new file mode 100644 index 0000000..715457f --- /dev/null +++ b/test.e2e/spec/fixtures/models/my-model.js @@ -0,0 +1,64 @@ +// Description: Extend `MyModel` model in Angular client generated service +// with static and prototype methods/properties +// +// Notes: +// * Docular comments will be included in Service doc as long as +// they moduleName and modelName are correct +// * Model must be declared in client-model.json to be generated/extended +module.exports = function (MyModel) { + + /** + * @ngdoc property + * @name lbServices.MyModel#STATUS + * @propertyOf lbServices.MyModel + * + * @description + * + * Status value constant + * + */ + MyModel.STATUS = 1; + + /** + * @ngdoc method + * @name lbServices.MyModel#findTwo + * @methodOf lbServices.MyModel + * + * @description + * + * Find two instances of the model from the data source + * + * @param {Function(Array., Object)=} successCb + * Success callback with two arguments: `value`, `responseHeaders`. + * + * @param {Function(Object)=} errorCb Error callback with one argument: + * `httpResponse`. + * + * @return {Array.} An empty reference that will be + * populated with the actual data once the response is returned + * from the server. + */ + MyModel.findTwo = function (successCb, errorCb) { + return MyModel.find({filter: {limit: 2}}, function(items, headers) { + if (successCb) + successCb(items, headers); + }, errorCb); + }; + + /** + * @ngdoc method + * @name lbServices.MyModel#getTotal + * @methodOf lbServices.MyModel + * + * @description + * + * Instance method computing total of `firstValue` and `secondValue` + * + * @return {Number} The total + */ + MyModel.prototype.getTotal = function () { + return this.first + this.second; + }; + + return MyModel; +}; diff --git a/test.e2e/spec/services.spec.js b/test.e2e/spec/services.spec.js index 313b515..70a9d2a 100644 --- a/test.e2e/spec/services.spec.js +++ b/test.e2e/spec/services.spec.js @@ -292,6 +292,51 @@ define(['angular', 'given', 'util'], function(angular, given, util) { }); }); + describe('MyModel $resource with extended custom logic', function() { + var $injector, MyModel; + before(function() { + return given.servicesForLoopBackApp( + { + models: { + MyModel: { + name: { type: String, required: true } , + first: { type: Number }, + second: { type: Number } + } + } + }) + .then(function(createInjector) { + $injector = createInjector(); + MyModel = $injector.get('MyModel'); + }); + }); + + it('has both extended static method and property', function() { + var staticMethod = MyModel.findTwo; + expect(staticMethod).to.be.a('Function'); + expect(MyModel).to.have.property('STATUS', 1); + }); + + it('instance can use extended prototype method', function() { + return MyModel.create({first: 1, second: 2}).$promise + .then(function() { + return MyModel.create({first: 1, second: 2}).$promise; + }) + .then(function() { + return MyModel.create({first: 1, second: 2}).$promise; + }) + .then(function() { + return MyModel.findTwo().$promise; + }) + .then(function(items) { + expect(items.length).to.equal(2); + expect(items[0].getTotal).to.be.a('Function'); + expect(items[0].getTotal()).to.equal(3); + }) + .catch(util.throwHttpError); + }); + }); + describe('$resource for model with funky name', function() { var $injector; before(function() { @@ -330,7 +375,7 @@ define(['angular', 'given', 'util'], function(angular, given, util) { } } }, - product: { + Product: { properties: { model: String } diff --git a/test.e2e/test-server.js b/test.e2e/test-server.js index f5a63dc..7cab5a4 100644 --- a/test.e2e/test-server.js +++ b/test.e2e/test-server.js @@ -3,7 +3,7 @@ The test server is an HTTP service allowing front-end tests running in a browser to setup a custom LoopBack instance and generate & access lb-services.js */ - +var path = require('path'); var express = require('express'); var loopback = require('loopback'); var generator = require('..'); @@ -105,7 +105,12 @@ masterApp.post('/setup', function(req, res, next) { } try { - servicesScript = generator.services(lbApp, name, apiUrl); + servicesScript = generator.services( + lbApp, + name, + apiUrl, + path.join(__dirname, 'spec/fixtures/model-config.json') + ); } catch (err) { console.error('Cannot generate services script:', err.stack); servicesScript = 'throw new Error("Error generating services script.");'; From d7679829daf913b31361f7fe0e5d40fc109662b5 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 26 Sep 2014 15:47:49 +0200 Subject: [PATCH 02/11] Enable non-REST models to be generated/extended * Some models in the server are not exposed but needed on the client * This enables generation of their Angular service (not resource) * Extention of those object models works the same as REST ones * These need to be declared in model-config with `dataSource: none` --- lib/services.js | 18 +++++++---- lib/services.template.ejs | 22 +++++++++++-- test.e2e/spec/fixtures/model-config.json | 3 ++ test.e2e/spec/fixtures/models/address.js | 41 ++++++++++++++++++++++++ test.e2e/spec/services.spec.js | 26 +++++++++++++++ 5 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 test.e2e/spec/fixtures/models/address.js diff --git a/lib/services.js b/lib/services.js index 0a3e8f2..e9474c2 100644 --- a/lib/services.js +++ b/lib/services.js @@ -220,12 +220,18 @@ function applyClientModelConfig(models, clientModelConfig) { } for (n in modelConfig) { - if (!(n in models) && modelConfig[n].dataSource == 'remote') { - console.log('Warning: Model ' + n + ' is expected to be REST exposed ' + - ' but is not found in app, so it will not be included ' + - ' in generated services file'); - delete modelConfig[n]; - continue; + if (n in models) { + models[n].isRemote = true; + } else { + if (modelConfig[n].dataSource == 'remote') { + console.log('Warning: Model ' + n + ' is expected to be REST exposed ' + + ' but is not found in app, so it will not be included ' + + ' in generated services file'); + delete modelConfig[n]; + continue; + } else { + models[n] = {isRemote: false}; + } } var meta = models[n]; diff --git a/lib/services.template.ejs b/lib/services.template.ejs index 4ac8aac..1474977 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -17,9 +17,10 @@ var module = angular.module(<%-: moduleName | q %>, ['ngResource']); <% for (var modelName in models) { var meta = models[modelName]; + if (typeof meta.isRemote === 'undefined' || meta.isRemote) { - // capitalize the model name - modelName = modelName[0].toUpperCase() + modelName.slice(1); + // capitalize the model name + modelName = modelName[0].toUpperCase() + modelName.slice(1); -%> /** * @ngdoc object @@ -245,6 +246,23 @@ module.factory( <% }); // forEach methods name -%> <% } // for each scope -%> +<% } else { // if meta.isRemote -%> + + /** + * @ngdoc object + * @name <%-: moduleName %>.<%-: modelName %> + * @object + * + * @description + * + * An object for interacting with the local `<%-: modelName %>` model. + * + */ + module.factory(<%-: modelName | q %>, [function() { + var R = function(){}; + +<% } // if meta.isRemote -%> + <% // Get available model extentions for this model and apply on resource object meta.extensions.forEach(function(extension) { -%> diff --git a/test.e2e/spec/fixtures/model-config.json b/test.e2e/spec/fixtures/model-config.json index 34b06ed..3ebde27 100644 --- a/test.e2e/spec/fixtures/model-config.json +++ b/test.e2e/spec/fixtures/model-config.json @@ -27,5 +27,8 @@ }, "Country": { "dataSource": "remote" + }, + "Address": { + "dataSource": null } } diff --git a/test.e2e/spec/fixtures/models/address.js b/test.e2e/spec/fixtures/models/address.js new file mode 100644 index 0000000..d581ff5 --- /dev/null +++ b/test.e2e/spec/fixtures/models/address.js @@ -0,0 +1,41 @@ +// Description: Extend `Address` model in Angular client generated service +// with static and prototype methods/properties +// +// Notes: +// * Docular comments will be included in Service doc as long as +// they moduleName and modelName are correct +// * Model must be declared in client-model.json to be generated/extended +module.exports = function (Address) { + + /** + * @ngdoc property + * @name lbServices.Address#contryCodes + * @propertyOf lbServices.Address + * + * @description + * + * Transform ISO contry code to readable string + * + */ + Address.countryCodes = { + 'us': 'United States', + 'fr': 'France' + }; + + /** + * @ngdoc method + * @name lbServices.Address#getFullAddress + * @methodOf lbServices.Address + * + * @description + * + * Instance method returning complete address + * + * @return {String} Complete address + */ + Address.prototype.getFullAddress = function () { + return this.recipient + ' ' + Address.countryCodes[this.country]; + }; + + return Address; +}; diff --git a/test.e2e/spec/services.spec.js b/test.e2e/spec/services.spec.js index 70a9d2a..18f2f3e 100644 --- a/test.e2e/spec/services.spec.js +++ b/test.e2e/spec/services.spec.js @@ -337,6 +337,32 @@ define(['angular', 'given', 'util'], function(angular, given, util) { }); }); + describe('Address local model with extended custom logic', function() { + var $injector, Address; + before(function() { + return given.servicesForLoopBackApp( + { + models: {} + }) + .then(function(createInjector) { + $injector = createInjector(); + Address = $injector.get('Address'); + }); + }); + + it('has an extended static property', function() { + expect(Address).to.have.property('countryCodes'); + }); + + it('instance can use extended prototype method', function() { + var address = new Address(); + address.recipient = 'Thomas'; + address.country = 'fr'; + expect(address.getFullAddress).to.be.a('Function'); + expect(address.getFullAddress()).to.equal('Thomas France'); + }); + }); + describe('$resource for model with funky name', function() { var $injector; before(function() { From aca7df233c443ebe43d8c11da1e936348920ad92 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 26 Sep 2014 17:12:56 +0200 Subject: [PATCH 03/11] Enable dependency injection defined client models * Client models might need other models/tools as dependency * In the server, model.js files start with `var dep = require('dep');` * The same model.js file will also get its dependencies by declaring them in model-config.json in the `dependencies` property * Example : `dependencies: ['User', '_', '$q']` * Dependencies must already have a provider in Angular ap where lbServices are used * REST resources and static models can have dependencies specified --- bower.json | 3 ++- lib/services.js | 1 + lib/services.template.ejs | 31 ++++++++++++++++++++--- test.e2e/given.js | 3 ++- test.e2e/spec/fixtures/model-config.json | 6 +++-- test.e2e/spec/fixtures/models/address.js | 8 ++++++ test.e2e/spec/fixtures/models/my-model.js | 8 ++++++ test.e2e/test-main.js | 7 +++++ 8 files changed, 59 insertions(+), 8 deletions(-) diff --git a/bower.json b/bower.json index 298a0f8..57099d2 100644 --- a/bower.json +++ b/bower.json @@ -12,6 +12,7 @@ "angular-resource": "~1.2.10", "angular-mocks": "~1.2.10", "requirejs": "~2.1.10", - "chai": "~1.8.1" + "chai": "~1.8.1", + "lodash": "~2.4.1" } } diff --git a/lib/services.js b/lib/services.js index e9474c2..5e1a5a9 100644 --- a/lib/services.js +++ b/lib/services.js @@ -236,6 +236,7 @@ function applyClientModelConfig(models, clientModelConfig) { var meta = models[n]; meta.extensions = []; + meta.dependencies = modelConfig[n].dependencies || []; // Supported file syntax for model ModelName : model-name.js var fsModelName = n.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); diff --git a/lib/services.template.ejs b/lib/services.template.ejs index 1474977..9d58256 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -15,7 +15,27 @@ var authHeader = 'authorization'; */ var module = angular.module(<%-: moduleName | q %>, ['ngResource']); -<% for (var modelName in models) { +<% + /* + * Returns an angular dependency-injection function opening block + * + * Example declareNgDeps(['$q', 'myDep'], false) : + * Returns : "['$q', 'myDep', function($q, myDep) {" + * + * Example declareNgDeps(['$q'], true) : + * Returns : "['$q', 'LoopBackResource', 'LoopBackAuth', '$injector', + function($q, LoopBackResource, LoopBackAuth, $injector) {" + */ + function declareNgDeps(deps, isRemote) { + deps = deps || []; + if (isRemote) { + deps = deps.concat(['LoopBackResource', 'LoopBackAuth', '$injector']); + } + return '[' + deps.map(JSON.stringify).join(',') + (deps.length? ',' : '') + + 'function(' + deps.join(',') + ') {'; + } + + for (var modelName in models) { var meta = models[modelName]; if (typeof meta.isRemote === 'undefined' || meta.isRemote) { @@ -45,8 +65,9 @@ var module = angular.module(<%-: moduleName | q %>, ['ngResource']); */ module.factory( <%-: modelName | q %>, - ['LoopBackResource', 'LoopBackAuth', '$injector', function(Resource, LoopBackAuth, $injector) { - var R = Resource( + <%-: declareNgDeps(meta.dependencies, true) %> + + var R = LoopBackResource( urlBase + <%-: meta.ctor.getFullPath() | q %>, <% /* Constructor arguments are hardcoded for now. @@ -258,7 +279,9 @@ module.factory( * An object for interacting with the local `<%-: modelName %>` model. * */ - module.factory(<%-: modelName | q %>, [function() { + module.factory(<%-: modelName | q %>, + <%-: declareNgDeps(meta.dependencies, false) %> + var R = function(){}; <% } // if meta.isRemote -%> diff --git a/test.e2e/given.js b/test.e2e/given.js index 5ae75d4..27808e3 100644 --- a/test.e2e/given.js +++ b/test.e2e/given.js @@ -60,7 +60,8 @@ define(['angular', 'angularMocks', 'angularResource'], function(angular) { .then(injectScriptAtUrl) .then(function() { return function createTestInjector() { - var injector = angular.injector(['ng', 'ngMockE2E', options.name]); + var injector = angular.injector(['ng', 'ngMockE2E', + 'lodash', options.name]); // Setup the mocked http provider to pass all $http requests // to our LoopBack server var $httpBackend = injector.get('$httpBackend'); diff --git a/test.e2e/spec/fixtures/model-config.json b/test.e2e/spec/fixtures/model-config.json index 3ebde27..07ab0fa 100644 --- a/test.e2e/spec/fixtures/model-config.json +++ b/test.e2e/spec/fixtures/model-config.json @@ -5,7 +5,8 @@ ] }, "MyModel": { - "dataSource": "remote" + "dataSource": "remote", + "dependencies": ["_"] }, "lower-case-not-an-identifier": { "dataSource": "remote" @@ -29,6 +30,7 @@ "dataSource": "remote" }, "Address": { - "dataSource": null + "dataSource": null, + "dependencies": ["_"] } } diff --git a/test.e2e/spec/fixtures/models/address.js b/test.e2e/spec/fixtures/models/address.js index d581ff5..05fd4ee 100644 --- a/test.e2e/spec/fixtures/models/address.js +++ b/test.e2e/spec/fixtures/models/address.js @@ -1,3 +1,9 @@ +// Dependencies +// Note: +// * The below values are relevant for server-side +// * For client-side, dependencies must be declared in model-config.json file +// * And they have to be available as an Angular provider in your app +var _ = require('lodash'); // Description: Extend `Address` model in Angular client generated service // with static and prototype methods/properties // @@ -34,6 +40,8 @@ module.exports = function (Address) { * @return {String} Complete address */ Address.prototype.getFullAddress = function () { + // Just to test dependency on lodash : + _.noop(); return this.recipient + ' ' + Address.countryCodes[this.country]; }; diff --git a/test.e2e/spec/fixtures/models/my-model.js b/test.e2e/spec/fixtures/models/my-model.js index 715457f..41109c3 100644 --- a/test.e2e/spec/fixtures/models/my-model.js +++ b/test.e2e/spec/fixtures/models/my-model.js @@ -1,3 +1,9 @@ +// Dependencies +// Note: +// * The below values are relevant for server-side +// * For client-side, dependencies must be declared in model-config.json file +// * And they have to be available as an Angular provider in your app +var _ = require('lodash'); // Description: Extend `MyModel` model in Angular client generated service // with static and prototype methods/properties // @@ -39,6 +45,8 @@ module.exports = function (MyModel) { * from the server. */ MyModel.findTwo = function (successCb, errorCb) { + // Just to test dependency on lodash : + _.noop(); return MyModel.find({filter: {limit: 2}}, function(items, headers) { if (successCb) successCb(items, headers); diff --git a/test.e2e/test-main.js b/test.e2e/test-main.js index 0d1333f..596ea8b 100644 --- a/test.e2e/test-main.js +++ b/test.e2e/test-main.js @@ -17,6 +17,7 @@ requirejs.config({ angular: 'bower_components/angular/angular', angularResource: 'bower_components/angular-resource/angular-resource', angularMocks: 'bower_components/angular-mocks/angular-mocks', + lodash: 'bower_components/lodash/dist/lodash', given: 'test.e2e/given', util: 'test.e2e/util' }, @@ -31,6 +32,7 @@ requirejs.config({ deps: ['angular'], exports: 'angular.mock' }, + 'lodash': { exports: '_' }, 'chai': { exports: 'chai' } }, @@ -44,3 +46,8 @@ requirejs.config({ require(['chai'], function(chai) { window.expect = chai.expect; }); + +require(['angular', 'lodash'], function(angular, _) { + // Make lodash module available as an Angular dependency in the app + angular.module('lodash', []).constant('_', _); +}); From df34c7d822dfff346e97cb9a1df145742a5388ff Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 26 Sep 2014 17:19:37 +0200 Subject: [PATCH 04/11] Fix deprecated express res.send(status,...) calls --- test.e2e/test-server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.e2e/test-server.js b/test.e2e/test-server.js index 7cab5a4..02e9e57 100644 --- a/test.e2e/test-server.js +++ b/test.e2e/test-server.js @@ -100,7 +100,7 @@ masterApp.post('/setup', function(req, res, next) { setupFn(lbApp, function(err, data) { if (err) { console.error('app setup function failed', err); - res.send(500, err); + res.status(500).send(err); return; } @@ -119,7 +119,7 @@ masterApp.post('/setup', function(req, res, next) { servicesScript += '\nangular.module(' + JSON.stringify(name) + ')' + '.value("testData", ' + JSON.stringify(data, null, 2) + ');\n'; - res.send(200, { servicesUrl: baseUrl + 'services?' + name }); + res.status(200).send({ servicesUrl: baseUrl + 'services?' + name }); }.bind(this)); }); From f50357ddc795c82fb6ea3effeee3a6527d17e795 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 26 Sep 2014 17:45:11 +0200 Subject: [PATCH 05/11] Fix issue with relative path and backward compat. --- lib/services.js | 3 ++- lib/services.template.ejs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/services.js b/lib/services.js index 5e1a5a9..ff6bf51 100644 --- a/lib/services.js +++ b/lib/services.js @@ -243,7 +243,8 @@ function applyClientModelConfig(models, clientModelConfig) { // Add this model extension functions to models description // in `extensions` array property for (var s in sources) { - var file = path.join(sources[s], fsModelName + '.js'); + var file = path.resolve(path.join(sources[s], fsModelName + '.js')); + console.log('CHECKING FILE', file); if (fs.existsSync(file)) meta.extensions.push(require(file)); } diff --git a/lib/services.template.ejs b/lib/services.template.ejs index 9d58256..1531fcb 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -287,7 +287,7 @@ module.factory( <% } // if meta.isRemote -%> <% // Get available model extentions for this model and apply on resource object - meta.extensions.forEach(function(extension) { -%> + (meta.extensions || []).forEach(function(extension) { -%> (<%-: extension %>)(R); From 17b7b59ea353dfd2df76481b0ef9951acb43b9d7 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Tue, 30 Sep 2014 14:38:55 +0200 Subject: [PATCH 06/11] Fix: debug information removal --- lib/services.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/services.js b/lib/services.js index ff6bf51..e686dde 100644 --- a/lib/services.js +++ b/lib/services.js @@ -191,7 +191,7 @@ function applyClientModelConfig(models, clientModelConfig) { var configFile = fs.readFileSync(clientModelConfig, {encoding: 'utf-8'}); modelConfig = JSON.parse(configFile); } catch(err) { - console.trace('FATAL: Provided client config file could not be loaded : ' + + console.error('FATAL: Provided client config file could not be loaded : ' + clientModelConfig, err); process.exit(1); } @@ -244,7 +244,6 @@ function applyClientModelConfig(models, clientModelConfig) { // in `extensions` array property for (var s in sources) { var file = path.resolve(path.join(sources[s], fsModelName + '.js')); - console.log('CHECKING FILE', file); if (fs.existsSync(file)) meta.extensions.push(require(file)); } From e805cd0aa270b7fd43d185deb18122646e3b9440 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Thu, 2 Oct 2014 19:56:12 +0200 Subject: [PATCH 07/11] Fix missing dev dependency on lodash --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index a7159fd..0a60b51 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "karma-mocha-reporter": "^0.2.6", "karma-phantomjs-launcher": "^0.1.2", "karma-requirejs": "^0.2.1", + "lodash": "^2.4.1", "loopback": "^2.0", "loopback-datasource-juggler": "^2.0", "mocha": "~1.18.0", From 9b36dc50d19b47a5845f7a86fd992576b38f86da Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Thu, 2 Oct 2014 19:56:44 +0200 Subject: [PATCH 08/11] Fix mistaken for-loop iteration on array --- lib/services.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/services.js b/lib/services.js index e686dde..7507668 100644 --- a/lib/services.js +++ b/lib/services.js @@ -242,8 +242,8 @@ function applyClientModelConfig(models, clientModelConfig) { // Add this model extension functions to models description // in `extensions` array property - for (var s in sources) { - var file = path.resolve(path.join(sources[s], fsModelName + '.js')); + for (var idx = 0; idx < sources.length; idx++) { + var file = path.resolve(path.join(sources[idx], fsModelName + '.js')); if (fs.existsSync(file)) meta.extensions.push(require(file)); } From 5715f37de503efbce72a6af299ecbc0b82af934b Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Wed, 8 Oct 2014 14:45:05 +0200 Subject: [PATCH 09/11] Improve non-rest object constructor --- lib/services.template.ejs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/services.template.ejs b/lib/services.template.ejs index 1531fcb..e756d81 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -282,7 +282,15 @@ module.factory( module.factory(<%-: modelName | q %>, <%-: declareNgDeps(meta.dependencies, false) %> - var R = function(){}; + // Simple object constructor that can be called + // with object properties as optional parameter + var R = function(opts) { + if (typeof opts === 'object') for (var prop in opts) { + if (opts.hasOwnProperty(prop)) { + this[prop] = opts[prop]; + } + } + }; <% } // if meta.isRemote -%> From a00ea7e3fdc24c58176d5249ef83de099737a879 Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Thu, 30 Oct 2014 12:17:56 +0100 Subject: [PATCH 10/11] Rebase PR, amend generateServices documentation --- lib/services.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/services.js b/lib/services.js index 7507668..573aa17 100644 --- a/lib/services.js +++ b/lib/services.js @@ -13,7 +13,7 @@ ejs.filters.q = function(obj) { * var generateServices = require('loopback-sdk-angular').services; * var app = require('./server/server'); * - * var client = generateServices(app, 'lbServices', '/api'); + * var client = generateServices(app, 'lbServices', '/api', clientModelConfig); * require('fs').writeFileSync('client/loopback.js', client, 'utf-8'); * ``` * @@ -21,7 +21,10 @@ ejs.filters.q = function(obj) { * @param {string=} ngModuleName A name for the generated Angular module. * Default: `lbServices`. * @param {string=} apiUrl The URL where the client can access the LoopBack - * server app. Default: `/`. + * server app. Default: `/`.* + * @param {Object} clientModelConfig Optional - The client model config that + * will determine what/how the services are generated. By default, (without + * providing any, all models wil be generated without options. * @returns {string} The generated javascript code. * @header generateServices */ From 52047a3d7657d88a5090cf37dd7baac9ed54630f Mon Sep 17 00:00:00 2001 From: Pandaiolo Date: Fri, 31 Oct 2014 17:46:34 +0100 Subject: [PATCH 11/11] Remove extraneous character from last rebase --- lib/services.template.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services.template.ejs b/lib/services.template.ejs index e756d81..44c460b 100644 --- a/lib/services.template.ejs +++ b/lib/services.template.ejs @@ -309,7 +309,7 @@ module.factory( * The name of the model represented by this $resource, * i.e. `<%- modelName %>`. */ - R.modelName = <%-: modelName | q %>;* + R.modelName = <%-: modelName | q %>; return R; }]);