javascript - Meteor autoform - compute value for hidden field -


in meteor autoform (using classic aldeed package: https://github.com/aldeed/meteor-autoform), have hidden field called 'score' value function of field called 'optionid'.

{{#autoform   class                = "autoform"   id                   = dataid   collection           = (getcollection 'datacoll')   doc                  = datadoc   type                 = "method-update"   meteormethod         = "datacoll.autoformupsert"   singlemethodargument = true   autosave             = true }}    {{> afquickfield     name         = 'optionid'     type         = "select-radio"     template     = "buttongroup"     options      = scoreoptions   }}    {{> afquickfield     name         = 'score'     type         = "hidden"     value        = selectedoptionscore   }}  {{/autoform}} 

the function selectedoptionscore depends on selected value of 'optionid' (using autoform.getfieldvalue)

template.formtemplate.helpers({   selectedoptionscore(): string {     const optionid = autoform.getfieldvalue('optionid');     const optionscore = somefunction(optionid);     return optionscore;   }, }); 

the problem on each autosave of form, value of 'score' "one change behind" value of 'optionid', meaning function computing 'score' correct, save mongo seems have happened before 'score' value updated, in mongo, 'score' has value of whatever right before 'optionid' last changed.

for example, if make 'score' not hidden such can modify it, modifying directly in webpage make value reflected correctly in mongo. modifying 'optionid' in webpage yields "one change behind" behavior.

is there way make meteor autoform field depend on field in same form, without having "lag behind" autosave 1 change always?

instead of using template helper calculate value of hidden score field, might want using autoform hook. instead of using selectedoptionscore template helper, define hook like:

autoform.addhooks(['your-form-id'], {   before: {     method-update(doc) {       doc.score = somefunction(doc.optionid);       return doc;     }   } }); 

this way before form saved calculated score set, , saved else.


Comments