-
Notifications
You must be signed in to change notification settings - Fork 2
/
AModel.js
265 lines (201 loc) · 7.3 KB
/
AModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Abstract model class
*
* @author Andrew Tereshko <[email protected]>
* @version 0.3.0
*/
define( ['./Observable'], function () {
'use strict';
/**
* @class yam.AModel
* @extends yam.Observable
*/
app.Observable.extend( 'yam.AModel',
/** @static **/
{
properties: [],
/**
* Setup method, MUST NOT BE OVERWRITTEN !
*
* Prepare getter \ setter methods
*
*/
setup: function( baseClass, fullName, staticProps, protoProps ){
for ( var variable in protoProps ) {
this._createGetSetFor( variable );
}
},
/**
* An utility method for determine identity property
*
* @return {String|Array}
*/
getIdentity: function(){
return 'id';
},
/**
* Example method to retrieve single model from storage
*
* @param {Number} identity
* @return jQuery.Promise
*/
get: function ( identity ) {
// Prepare troops
var deferred = $.Deferred();
// for watched model
// if ( !app.ModelWatcher.has( this, identity ) ) {
//
// // here comes request (ajax or something...)
// // don't forget to resolve deferred object ( or you will make him sad )
// }
// Landing
return deferred.promise();
},
/**
* Example method to retrieve model collection from storage
*
* @param {Array} identityArray
* @return jQuery.Promise
*/
getList: function ( identityArray ) {
// just as example, Mostly same as get()
var deferred = $.Deferred();
return deferred.promise();
},
/**
* Private utility method
*
* @param varName
*/
_getMethodEnding: function (varName) {
return varName.charAt(0).toUpperCase() + varName.substr(1, varName.length - 1);
},
_createGetSetFor: function( variable ){
if ( typeof this.prototype[variable] === "function" ) return;
if ( variable.charAt(0) == '_' ) return;
var methodEnding = this._getMethodEnding( variable );
// make getter if not exists
if ( undefined === this.prototype[ "get" + methodEnding ] ) {
this.prototype[ "get" + methodEnding ] = function () {
return this[ variable ];
};
}
//making setter if not exists
if ( undefined === this.prototype[ "set" + methodEnding ] ) {
this.prototype[ "set" + methodEnding ] = function (value) {
return this._set( variable, value );
};
}
// store to properties array
this.properties.push( variable );
},
factory: function( data ) {
var model = new this();
if ( ( data instanceof Object ) )
{
model.fill( data );
}
return model;
}
},
/** @prototype **/
{
/**
* base identity property
*/
id:undefined,
/**
* Array modified properties.
*/
_modifiedProperties: undefined,
/**
* Class constructor
*
* @see fill method
*
* @param values
*/
init: function ( values ) {
this.fill( values );
},
/**
* Param values must be object with param : value pairs
* By default - values will be assigned to new object properties through setters
* @param values
*/
fill: function( values ) {
for (var param in values) {
var setter = "set" + this.Class._getMethodEnding( param );
if (undefined === this[ setter ] || typeof this[setter] !== "function") continue;
this[setter]( values[ param ] );
}
},
/**
* Gets value of identity property
*
* @return {String}
*/
getIdentityValue: function(){
return this[ this.Class.getIdentity() ] !== undefined ? this[ this.Class.getIdentity() ] : undefined;
},
/**
* Observable realisation
*
* @param variable
* @param value
* @param oldValue
* @private
*/
_triggerProperty: function( variable, value, oldValue ){
$( [this] ).triggerHandler( "propertyChange", { path: variable, value: value, oldValue: oldValue } );
//$( this ).triggerHandler( "objectChange", { property: variable, value: value, oldValue: oldValue } );
//console.log( "triggered", "propertyChange", { path: variable, value: value, oldValue: oldValue } );
},
/**
* Base setter
*
* @param variable
* @param value
* @returns {boolean}
* @private
*/
_set: function( variable, value ){
var oldValue = this[ variable ];
this[ variable ] = value;
// If oldValue is not undefined, then property has been modified early.
// And we must save name of modified variable.
if( oldValue !== undefined ) {
if( this._modifiedProperties instanceof Array ) {
this._modifiedProperties.push( variable );
} else {
this._modifiedProperties = [ variable ];
}
}
this._triggerProperty( variable, value, oldValue );
return true;
},
/**
* Get array of modified properties.
*
* @return {Array}
*/
getModifiedProperties: function() {
return ( this._modifiedProperties instanceof Array ) ? this._modifiedProperties : [] ;
},
propertiesData: function(){
var data = {},
propertyName;
for( var len = this.Class.properties.length; len >= 0; --len ){
propertyName = this.Class.properties[ len ];
if( propertyName === undefined || !this.hasOwnProperty( propertyName) ) continue;
data[ propertyName ] = this[ propertyName ];
}
return data;
}
}
);
//backward compatibility
if( undefined === window.app ) window.app = {};
window.app.AModel = yam.AModel;
return yam.AModel;
});