/
Class.Mutators.StoredInstances.js
67 lines (57 loc) · 1.73 KB
/
Class.Mutators.StoredInstances.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
/*
---
name: Class.Mutators.StoredInstances
description: Allow classes to stored instances that has been created. Note that because this
contains static data and methods, any subclass will also need to include this if the parent
class has it.
license: MIT-style license
authors:
- Duc Tri Le
requires:
- Core/MooTools
provides:
- Class.Mutators.StoredInstances
...
*/
Class.Mutators.StoredInstances = function() {
this.extend({
/**
* @type {Object} All the instances that has been stored.
*/
$instances: {},
/**
* Get a stored instance.
*
* @param id {String} The identifier that the instance was stored by.
* @returns {Mixed} The stored instance or null if it does not exists.
*/
retrieveInstance: function(id) {
return [this.$instances[id]].pick();
}
}).implement({
/**
* Get a stored instance. Note that this simply allow you to access the static version of
* this method on the instance itself.
*
* @param id {String} The identifier that the instance was stored by.
* @returns {Mixed} The stored instance or null if it does not exists.
*/
retrieveInstance: function(id) {
return this.$caller.$owner.retrieveInstance(id);
},
/**
* Store this instance. Note that this will NOT overwrite another instance if the provided
* id already exists.
*
* @param id {String} The identifier for this instance.
* @returns {Mixed} Returns true if the instance was successfully stored. If the another
* instance was already stored with the provided id, that instance is returned.
*/
storeInstance: function(id) {
var instance = this.retrieveInstance(id);
if(instance) { return instance; }
this.$caller.$owner.$instances[id] = this;
return true;
}
});
};