|
1 | 1 | (function() { |
2 | 2 | var client = require('swagger-client'), |
| 3 | + util = require('util'), |
3 | 4 | config = { |
4 | 5 | fileOutput: null, |
5 | 6 | filePath: null, |
|
39 | 40 | model, |
40 | 41 | modelName, |
41 | 42 | property, |
42 | | - script; |
| 43 | + script, |
| 44 | + scriptModel, |
| 45 | + scriptValidation; |
| 46 | + script = []; |
43 | 47 | if (swagger.ready === true) { |
44 | | - script = [ |
45 | | - '(function () {' |
46 | | - ]; |
47 | 48 | for (apiName in swagger.apis) { |
48 | 49 | api = swagger.apis[apiName]; |
49 | 50 | for (modelName in api.models) { |
50 | 51 | model = api.models[modelName]; |
51 | | - script.push.apply(script, [ |
52 | | - ' var ' + model.name + ' = Backbone.Model.extend({', |
53 | | - ' defaults: {' |
54 | | - ]); |
| 52 | + scriptModel = []; |
| 53 | + scriptValidation = []; |
55 | 54 | for (var _i = 0, _len = model.properties.length; _i < _len; _i++) { |
56 | 55 | property = model.properties[_i]; |
57 | | - script.push(' ' + property.name + ': ' + property.defaultValue); |
| 56 | + if (scriptModel.length > 0) { |
| 57 | + scriptModel[scriptModel.length - 1] += ','; |
| 58 | + } |
| 59 | + if (property.defaultValue !== null) { |
| 60 | + property.defaultValue = util.format('\'%s\'', property.defaultValue); |
| 61 | + } |
| 62 | + scriptModel.push(util.format(' %s: %s', property.name, property.defaultValue)); |
| 63 | + if (property.required) { |
| 64 | + scriptValidation.push.apply(scriptValidation, [ |
| 65 | + util.format(' if (!attrs.%s) {', property.name), |
| 66 | + util.format(' return \'Please fill %s field.\';', property.name), |
| 67 | + ' }' |
| 68 | + ]); |
| 69 | + } |
58 | 70 | } |
59 | 71 | script.push.apply(script, [ |
| 72 | + ' var ' + model.name + ' = Backbone.Model.extend({', |
| 73 | + ' urlRoot: \'' + api.basePath + '/' + api.path + '\',', |
| 74 | + ' url: function() {', |
| 75 | + ' return this.urlRoot + \'/\' + this.id;', |
| 76 | + ' },', |
| 77 | + ' defaults: {', |
| 78 | + scriptModel.join('\n'), |
| 79 | + ' },', |
| 80 | + ' validate: function (attrs) {', |
| 81 | + scriptValidation.join('\n'), |
60 | 82 | ' }', |
61 | 83 | ' });' |
62 | 84 | ]); |
63 | 85 | } |
64 | 86 | } |
65 | | - script.push('})();'); |
66 | 87 | return reply(script.join('\n')).type('application/javascript'); |
67 | 88 | } |
68 | 89 | }, |
|
0 commit comments