UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2
3/**
4@class EntityPool
5@module base-domain
6 */
7var EntityPool;
8
9EntityPool = (function() {
10 function EntityPool() {}
11
12
13 /**
14 Register an entity to pool
15 @method set
16 @param {Entity} model
17 */
18
19 EntityPool.prototype.set = function(model) {
20 var Model, modelName;
21 Model = model.constructor;
22 if (!Model.isEntity || ((model != null ? model.id : void 0) == null)) {
23 return;
24 }
25 modelName = Model.getName();
26 if (EntityPool.prototype[modelName]) {
27 throw new Error("invalid model name " + modelName);
28 }
29 if (this[modelName] == null) {
30 this[modelName] = {};
31 }
32 return this[modelName][model.id] = model;
33 };
34
35
36 /**
37 Get registred models by model name and id
38
39 @method get
40 @param {String} modelName
41 @param {String} id
42 @return {Entity}
43 */
44
45 EntityPool.prototype.get = function(modelName, id) {
46 var ref;
47 return (ref = this[modelName]) != null ? ref[id] : void 0;
48 };
49
50
51 /**
52 Clear all the registered entities
53
54 @method clear
55 */
56
57 EntityPool.prototype.clear = function() {
58 var id, modelName, models, results;
59 results = [];
60 for (modelName in this) {
61 models = this[modelName];
62 for (id in models) {
63 delete models[id];
64 }
65 results.push(delete models[modelName]);
66 }
67 return results;
68 };
69
70 return EntityPool;
71
72})();
73
74module.exports = EntityPool;