-
-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathautoform-formdata.js
54 lines (48 loc) · 1.3 KB
/
autoform-formdata.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
import { Tracker } from 'meteor/tracker'
/*
* Tracks form data with reactivity. This is similar to
* ReactiveDict, but we need to store typed objects and
* keep their type upon retrieval.
*/
export class FormData {
constructor () {
const self = this
self.forms = {}
}
/**
* Initializes tracking for a given form, if not already done.
* @param {String} formId The form's `id` attribute
*/
initForm (formId) {
const self = this
if (self.forms[formId]) {
return
}
self.forms[formId] = {
sourceDoc: null,
deps: {
sourceDoc: new Tracker.Dependency()
}
}
}
/**
* Gets or sets a source doc for the given form. Reactive.
* @param {String} formId The form's `id` attribute
* @param {MongoObject|null} sourceDoc The mDoc for the form or `null` if no doc.
* @returns {MongoObject|undefined} Returns the form's MongoObject if getting.
*/
sourceDoc (formId, sourceDoc) {
const self = this
self.initForm(formId)
if (sourceDoc || sourceDoc === null) {
// setter
self.forms[formId].sourceDoc = sourceDoc
self.forms[formId].deps.sourceDoc.changed()
}
else {
// getter
self.forms[formId].deps.sourceDoc.depend()
return self.forms[formId].sourceDoc
}
}
}