From eefb38b17869feab8e02649a4d0e2490acf0364e Mon Sep 17 00:00:00 2001 From: elenius Date: Fri, 5 Feb 2016 18:33:49 -0800 Subject: [PATCH] Use prototypes for model methods Fixes #2044. --- .../main/resources/Javascript/model.mustache | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache index ee30a99bb02..49b79bbac07 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/model.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/model.mustache @@ -23,7 +23,6 @@ * {{description}} **/{{/description}} var {{classname}} = function {{classname}}({{#mandatory}}{{this}}{{^-last}}, {{/-last}}{{/mandatory}}) { {{#parent}}/* extends {{{parent}}}*/{{/parent}} - var self = this; {{#vars}} /**{{#description}} * {{{description}}}{{/description}} @@ -32,43 +31,43 @@ * minimum: {{minimum}}{{/minimum}}{{#maximum}} * maximum: {{maximum}}{{/maximum}} **/ - self['{{baseName}}'] = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}}; + this['{{baseName}}'] = {{#required}}{{name}}{{/required}}{{^required}}{{{defaultValue}}}{{/required}}; {{/vars}} + }; - self.constructFromObject = function(data) { - if (!data) { - return this; - } - {{#vars}} - self['{{baseName}}']{{{defaultValueWithParam}}} - {{/vars}} + {{classname}}.prototype.constructFromObject = function(data) { + if (!data) { return this; } - {{#vars}} - /**{{#description}} - * get {{{description}}}{{/description}}{{#minimum}} - * minimum: {{minimum}}{{/minimum}}{{#maximum}} - * maximum: {{maximum}}{{/maximum}} - * @return {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> - **/ - self.{{getter}} = function() { - return self['{{baseName}}']; - } - - /**{{#description}} - * set {{{description}}}{{/description}} - * @param {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> {{name}} - **/ - self.{{setter}} = function({{name}}) { - self['{{baseName}}'] = {{name}}; - } + this['{{baseName}}']{{{defaultValueWithParam}}} {{/vars}} + return this; + } - self.toJson = function() { - return JSON.stringify(self); - } - }; + {{#vars}} + /**{{#description}} + * get {{{description}}}{{/description}}{{#minimum}} + * minimum: {{minimum}}{{/minimum}}{{#maximum}} + * maximum: {{maximum}}{{/maximum}} + * @return {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> + **/ + {{classname}}.prototype.{{getter}} = function() { + return this['{{baseName}}']; + } + + /**{{#description}} + * set {{{description}}}{{/description}} + * @param {{=<% %>=}}{<% datatypeWithEnum %>}<%={{ }}=%> {{name}} + **/ + {{classname}}.prototype.{{setter}} = function({{name}}) { + this['{{baseName}}'] = {{name}}; + } + {{/vars}} + + {{classname}}.prototype.toJson = function() { + return JSON.stringify(this); + } if (module) { module.{{classname}} = {{classname}};