forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-formdata.js
57 lines (50 loc) · 1.41 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
55
56
57
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 const FormData = function FormData () {
const self = this
self.forms = {}
}
/**
* Initializes tracking for a given form, if not already done.
* @param {String} formId The form's `id` attribute
*/
FormData.prototype.initForm = function (formId) {
const self = this
if (self.forms[formId]) {
return
}
self.forms[formId] = {
sourceDoc: null,
deps: {
sourceDoc: new Tracker.Dependency()
}
}
}
/**
* Initializes tracking for a given form, if not already done.
* @param {String} formId The form's `id` attribute
*/
/**
* 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.
*/
FormData.prototype.sourceDoc = function (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
}
}