forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-preserve.js
50 lines (45 loc) · 1.44 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
/**
* @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) || {};
Reload._onMigrate(migrationName, function () {
return [true, self._retrieveRegisteredDocuments()];
});
}
};
FormPreserve.prototype.getDocument = function (formId) {
var self = this;
if (! _.has(self.retrievedDocuments, formId))
return false;
else
return self.retrievedDocuments[formId];
};
FormPreserve.prototype.saveDocument = function (formId) {
this.retrievedDocuments[formId] = this.registeredForms[formId]();
}
FormPreserve.prototype.registerForm = function (formId, retrieveFunc) {
this.registeredForms[formId] = retrieveFunc;
};
FormPreserve.prototype.unregisterForm = function (formId) {
delete this.registeredForms[formId];
};
FormPreserve.prototype._retrieveRegisteredDocuments = function () {
res = {};
_.each(this.registeredForms, function (retrieveFunc, formId) {
res[formId] = retrieveFunc();
});
return res;
};