Change the value of the reactive variable on the hook in Meteor

I have

Template.templateName.onCreated(function() { this.variableName = new ReactiveVar; this.variableName.set(true); }); 

and in templateName I have autoform . I need to set the autoform variable variableName to false when autoform sent.

I tried

 AutoForm.hooks({ myForm: { onSuccess: function(operation, result) { this.variableName.set(false); }, } }); 

but it does not work since this. does not refer to template templateName , as happens in helpers and events. This would work if I used sessions instead, as they are not limited / limited to specific templates.

What can I do to change the reactive variable in a car object?

I also tried

 AutoForm.hooks({ myForm: { onSuccess: function(operation, result) { this.template.variableName.set(false); this.template.parent.variableName.set(false); this.template.parent().variableName.set(false); this.template.parentData.variableName.set(false); this.template.parentData().variableName.set(false); this.template.parentView.variableName.set(false); this.template.parentView().variableName.set(false); }, } }); 

When using console.log(this.template) it prints an object. If I use console.log(this.template.data) , I get

 Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…} 

I use the variable variableName to determine whether to show an editable form or a good presentation of the data to the user. Perhaps there is another better way to do this.

+6
source share
2 answers

Edit onCreated:

 Template.templateName.onCreated(function() { this.variableName = new ReactiveVar(false); }); 

You can add a helper function to grab a template instance:

 Template.templateName.helpers({ getVariableName: function() { return Template.instance().variableName.get(); } }); 

and then you can invoke forms in your logic

 AutoForm.hooks({ myForm: { onSuccess: function(operation, result) { Template.getVariableName.set(false); }, } }); 

MeteorChef has an excellent article on reactive variables / dictionaries. Reactive Variables .

+1
source

If people stumble on this decision, based on this thread, I can get access to parents Reactive Dict / Reactive Variable after installing aldeed: template extension like this

 AutoForm.hooks({ postFormId: { //onSuccess hook of my post form onSuccess: function(formType, result) { this.template.parent().reactiveDictVariable.set('imgId', 'newValueSetOnSuccess'); //Or reactive var, and depending on your hierarchy //this.template.parent().parent().yourReactiveVar.set(undefined); } } }); 

Here is the Html and JS link for reference.

 <template name="postsTemplate"> {{#autoForm schema=postFormSchema id="postFormId" type="method" meteormethod="addPost"}} <!-- other autoform stuff --> This is reactive variable used in template -- {{imgId}} {{/autoForm}} </template> Template.postsTemplate.created = function() { //Using reactive-dict package here this.reactiveDictVariable = new ReactiveDict(); this.reactiveDictVariable.set('imgId', undefined); }; Template.posts.events( "change .some-class": function(event, template) { //other stuff template.postImgState.set('imgId', 'something'); } 
0
source

All Articles