forked from mamonth/yam-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AModel.js
197 lines (150 loc) · 5.72 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
/**
* Abstract model class
*
* @author Andrew Tereshko <[email protected]>
* @version 0.3.0
*/
define("app/AModel", ["app/Observable","app/ModelWatcher","app/ModelList"], function () {
"use strict";
/**
* @class app.AModel
*/
app.Observable.extend("app.AModel",
/* @static */
{
/**
* 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();
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 );
};
}
}
},
/* @prototype */
{
/**
* base identity property
*/
id:undefined,
/**
* Array modified properties.
*/
_modifiedProperties: undefined,
/**
* Class constructor
*
* Param values must be object with param : value pairs
* By default - values will be assigned to new object properties through setters
*
* @param values
*/
init: 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;
},
_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 } );
},
_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 : [] ;
}
}
);
});