1- // Model class to be subclassed by an actual model
1+ /* eslint-disable func-names */
22
33import Error from './error' ;
44
@@ -14,8 +14,21 @@ const REGEXP_ALIASED = /^([A-Za-z0-9-_]*)\s+(\{\}|\[\])?(\w+)$/i;
1414const REGEXP_NOTALIASED = / ^ ( ) ( \{ \} | \[ \] ) ? ( \w + ) $ / i;
1515
1616// ////////////////////////////////////////////////////////////////////////////
17- // MODEL CLASS
1817
18+ /**
19+ * Model manages data transfer between provider, form and internal representation.
20+ * @class
21+ * @classdesc This class is used as a base class to a model. You should extend
22+ * this class and call the define method to describe the data model properties. When constructing
23+ * a model, you can then access your properties as you would any other object.
24+ *
25+ * @arg {Object} data - The data which is parsed and used to construct the model instance.
26+ *
27+ * @property {string } $json - Returns JSON representation of the model values.
28+ * @property {string } $className - Returns the class name of the model.
29+ *
30+ * @throws Error
31+ */
1932export default class Model {
2033 constructor ( data ) {
2134 // Get prototype
@@ -32,6 +45,22 @@ export default class Model {
3245 this . $setall ( data ) ;
3346 }
3447
48+ /**
49+ * Define the object model. The model supports string, number, boolean and date
50+ * as native values, and map, object and array as collections. You can define
51+ * the model as an array of properties, each property is a string with an optional
52+ * alias (the data key of the external representation) and the internal representation.
53+ * For example, to define a string use "string" as the property, or for example "id string"
54+ * if the external representation uses 'id' as the key. To define an object as a
55+ * model member, use the name of the model (ie, 'User'). For a map, use '{}' before the
56+ * type and for an array use '[]' before the type.
57+ *
58+ * @arg {function} classConstructor - The constructor for the model
59+ * @arg {Object.<string,string>} classProps - The definition of the model properties
60+ * @arg {string=} className - The name of the class referred to in class properties.
61+ * Uses constructor name if not given.
62+ * @throws Error
63+ */
3564 static define ( classConstructor , classProps , className ) {
3665 if ( typeof classConstructor !== 'function' ) {
3766 throw new Error ( 'Called define without a class constructor' ) ;
@@ -48,6 +77,42 @@ export default class Model {
4877 Model . models [ classKey ] = proto ;
4978 }
5079
80+ /**
81+ * @method Model#$get
82+ * @arg {string} key - The key of the property
83+ * @returns {string|number|boolean|Date|Map|Array|Model|undefined }
84+ * @desc Return a property value or undefined
85+ */
86+ /**
87+ * @method Model#$set
88+ * @arg {string} key - The key of the property
89+ * @arg {string|number|boolean|Date|Map|Array|Model|undefined} value - The value
90+ * for the property
91+ * @returns {string|number|boolean|Date|Map|Array|Model|undefined }
92+ * @desc Set a property value for a key
93+ */
94+ /**
95+ * @method Model#$getall
96+ * @returns {Object }
97+ * @desc Get all property values that can be transmitted stored in external representation.
98+ * Does not include values which are undefined.
99+ */
100+ /**
101+ * @method Model#$setall
102+ * @desc Set all property values from external representation. Replaces all existing
103+ * property values.
104+ * @returns {Object }
105+ */
106+ /**
107+ * @method Model#toString
108+ * @desc Return object in string form, for debugging.
109+ * @returns {string }
110+ */
111+ /**
112+ * @method Model#$equals
113+ * @desc Checks for equality between this object and another model.
114+ * @returns {boolean }
115+ */
51116 static $newproto ( classKey , classProps , className ) {
52117 const proto = { } ;
53118
@@ -99,21 +164,16 @@ export default class Model {
99164 } ) ;
100165 } ) ;
101166
102- // $get function
103- // eslint-disable-next-line func-names
104167 proto . $get = function ( key ) {
105168 return this . $data [ key ] ;
106169 } ;
107170
108- // $set function
109- // eslint-disable-next-line func-names
110171 proto . $set = function ( key , value ) {
111172 const v = this . $type . get ( key ) ;
112173 this . $data [ key ] = Model . $cast ( value , v . collection , v . primitive , v . model ) ;
174+ return this . $data [ key ] ;
113175 } ;
114176
115- // $getall function
116- // eslint-disable-next-line func-names
117177 proto . $getall = function ( ) {
118178 const obj = { } ;
119179 this . $type . forEach ( ( v , k ) => {
@@ -125,8 +185,6 @@ export default class Model {
125185 return obj ;
126186 } ;
127187
128- // $setall function
129- // eslint-disable-next-line func-names
130188 proto . $setall = function ( data ) {
131189 if ( typeof data !== 'object' ) {
132190 throw new Error ( `Constructor requires object for ${ this . constructor . name } ` ) ;
@@ -137,8 +195,6 @@ export default class Model {
137195 } ) ;
138196 } ;
139197
140- // toString function
141- // eslint-disable-next-line func-names
142198 proto . toString = function ( ) {
143199 let str = `<${ this . $className } ` ;
144200 this . $type . forEach ( ( _ , k ) => {
@@ -148,8 +204,6 @@ export default class Model {
148204 return `${ str } >` ;
149205 } ;
150206
151- // $equals function
152- // eslint-disable-next-line func-names
153207 proto . $equals = function ( other ) {
154208 if ( ! other ) {
155209 return false ;
0 commit comments