forked from mamonth/yam-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelWatcher.js
130 lines (93 loc) · 4.12 KB
/
ModelWatcher.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
/**
* Model Watcher
*
* @author Andrew Tereshko <[email protected]>
* @version 0.3.0
*/
define("app/ModelWatcher", [ /* "app/Model" */ ], function () {
"use strict";
/* experimenting on js exception handle */
window.InvalidArgumentException = function( message ){
this.message = message || "";
};
window.InvalidArgumentException.prototype = new Error();
window.InvalidArgumentException.prototype.constructor = window.InvalidArgumentException;
window.InvalidArgumentException.prototype.name = 'InvalidArgumentException';
/**
* @class app.ModelWatcher
*/
$.Class.extend("app.ModelWatcher",
/* @static */
{
/**
* Object
*/
_storage: {},
/**
* Register model instance to watcher
*
* @param {app.AModel} modelInstance
*/
register: function( modelInstance ){
if( !( modelInstance instanceof app.AModel ) || undefined === modelInstance.Class.fullName )
throw new InvalidArgumentException( this.fullName + " can handle only app.AModel instances");
var identity = modelInstance.getIdentityValue();
if( this.has( modelInstance.Class, identity ) )
throw new Error( modelInstance.Class.fullName + " instance with identity " + identity + " already registered");
if( undefined === this._storage[ modelInstance.Class.fullName ] )
this._storage[ modelInstance.Class.fullName ] = {};
this._storage[ modelInstance.Class.fullName ][ identity ] = modelInstance;
},
/**
* Check if model instance already registered
*
* @param classDef
* @param identity
*/
has: function( classDef, identity ){
if( undefined === classDef.fullName )
throw new InvalidArgumentException( this.fullName + " can handle only classes inherited from app.AModel ");
return !( undefined === this._storage[ classDef.fullName ] || undefined === this._storage[ classDef.fullName ][ identity ] );
},
/**
*
* @param classDef
* @param identity
* @return {app.AModel}
*/
get: function( classDef, identity ){
if( undefined === classDef.fullName )
throw new InvalidArgumentException( this.fullName + " can handle only classes inherited from app.AModel.");
return this.has( classDef, identity ) ? this._storage[ classDef.fullName ][ identity ] : undefined;
},
//Возвращает ModelList
getList: function( classDef, identityArr ){
var coll = new app.ModelList( classDef );
if ( identityArr instanceof Array && identityArr.length > 0){
for( var id in identityArr ){
var mdl = this.get(classDef, identityArr[ id ]);
if ( mdl !== undefined ) {
coll.push( mdl )
}
}
}
return coll;
},
/**
* @param {app.AModel} modelInstance
*/
unregister: function( modelInstance ){
if( ! this.has( modelInstance.Class, modelInstance.getIdentityValue() ) )
throw new InvalidArgumentException( "Model " + modelInstance.Class.fullName + " with id " + modelInstance.getIdentityValue() + " was not registered." );
var storage = {};
for( var identity in this._storage[ modelInstance.Class.fullName ] ){
if( identity == modelInstance.getIdentityValue() ) continue;
storage[ identity ] = this._storage[ modelInstance.Class.fullName ][ identity ];
}
this._storage[ modelInstance.Class.fullName ] = storage;
}
},
{
}
);
});