UNPKG

956 Btext/coffeescriptView Raw
1'use strict'
2
3###*
4@class EntityPool
5@module base-domain
6###
7class EntityPool
8
9 constructor: ->
10
11 ###*
12 Register an entity to pool
13 @method set
14 @param {Entity} model
15 ###
16 set: (model) ->
17
18 Model = model.constructor
19 return if not Model.isEntity or not model?.id?
20
21 modelName = Model.getName()
22
23 throw new Error("invalid model name #{modelName}") if EntityPool::[modelName]
24
25 @[modelName] ?= {}
26 @[modelName][model.id] = model
27
28
29 ###*
30 Get registred models by model name and id
31
32 @method get
33 @param {String} modelName
34 @param {String} id
35 @return {Entity}
36 ###
37 get: (modelName, id) ->
38
39 @[modelName]?[id]
40
41
42 ###*
43 Clear all the registered entities
44
45 @method clear
46 ###
47 clear: ->
48 for modelName, models of @
49 for id of models
50 delete models[id]
51 delete models[modelName]
52
53module.exports = EntityPool