forked from gregory/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-preserve.js
72 lines (62 loc) · 2.04 KB
/
form-preserve.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
/**
* @constructor
* @private
* @param {String} migrationName
*
* Internal helper object to preserve form inputs across Hot Code Push
* and across "pages" navigation if the option is enabled.
*/
FormPreserve = function formPreserveConstructor(migrationName) {
var self = this;
if (! _.isString(migrationName))
throw Error("You must define an unique migration name of type String");
self.registeredForms = {};
self.retrievedDocuments = {};
if (Package.reload) {
var Reload = Package.reload.Reload;
self.retrievedDocuments = Reload._migrationData(migrationName) || '{}';
// Currently migration does not seem to support proper storage
// of Date type. It comes back as a string, so we need to store
// EJSON instead.
if (typeof self.retrievedDocuments === 'string') {
self.retrievedDocuments = EJSON.parse(self.retrievedDocuments);
}
Reload._onMigrate(migrationName, function () {
var doc = self._retrieveRegisteredDocuments();
return [true, EJSON.stringify(doc)];
});
}
};
FormPreserve.prototype.getDocument = function (formId) {
var self = this, doc;
if (! _.has(self.retrievedDocuments, formId)) {
return false;
}
return self.retrievedDocuments[formId];
};
FormPreserve.prototype.clearDocument = function (formId) {
delete this.retrievedDocuments[formId];
};
FormPreserve.prototype.registerForm = function (formId, retrieveFunc) {
this.registeredForms[formId] = retrieveFunc;
};
FormPreserve.prototype.formIsRegistered = function (formId) {
return !!this.registeredForms[formId];
};
FormPreserve.prototype.unregisterForm = function (formId) {
delete this.registeredForms[formId];
delete this.retrievedDocuments[formId];
};
FormPreserve.prototype.unregisterAllForms = function () {
var self = this;
self.registeredForms = {};
self.retrievedDocuments = {};
};
FormPreserve.prototype._retrieveRegisteredDocuments = function () {
var self = this;
res = {};
_.each(self.registeredForms, function (retrieveFunc, formId) {
res[formId] = retrieveFunc();
});
return res;
};