I have
Template.templateName.onCreated(function() {
this.variableName = new ReactiveVar;
this.variableName.set(true);
});
and in templateName
I have an autoform
. I need to set the reactive variable variableName
to false
when the autoform
is submitted.
在templateName中我有一个autoform。我需要在提交autoform时将反应变量variableName设置为false。
I have tried
我试过了
AutoForm.hooks({
myForm: {
onSuccess: function(operation, result) {
this.variableName.set(false);
},
}
});
but it doesn't work since this.
does not refer to the template templateName
as it does in helpers and events. It would have worked if I used sessions instead since these are not limited/scoped to specific templates.
但是从那以后它就不起作用了。不会像帮助程序和事件中那样引用模板templateName。如果我使用会话,它会起作用,因为它们不限于特定模板。
What can I do to change the reactive variable in an autoform hook?
如何更改autoform钩子中的反应变量?
I have 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 does print an object. If I use console.log(this.template.data)
I get
使用console.log(this.template)时,它会打印一个对象。如果我使用console.log(this.template.data)我得到
Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}
I use the reactive variable variableName
to determine whether to either show the editable form or the nice presentation of data for the user. Maybe there is another better way to do this.
我使用反应变量variableName来确定是显示可编辑的表单还是显示用户的数据的良好表示。也许有另一种更好的方法来做到这一点。
2 个解决方案
#1
1
Edit of onCreated:
编辑onCreated:
Template.templateName.onCreated(function() {
this.variableName = new ReactiveVar(false);
});
You may want to add a helper function to grab the Template instance:
您可能需要添加辅助函数来获取Template实例:
Template.templateName.helpers({
getVariableName: function() {
return Template.instance().variableName.get();
}
});
and then you could potentially call within your form logic
然后你可以在你的表单逻辑中调用
AutoForm.hooks({
myForm: {
onSuccess: function(operation, result) {
Template.getVariableName.set(false);
},
}
});
The MeteorChef has a great article about reactive variables/dictionaries Reactive Variables.
MeteorChef有一篇关于反应变量/字典Reactive Variables的文章很棒。
#2
0
If people stumble upon this for a solution, based this thread I as able to access the parents Reactive Dict/Reactive Variable after installing aldeed:template-extension like so
如果人们偶然发现这个解决方案,基于这个线程,我可以在安装aldeed:template-extension之后访问父节点Reactive Dict / Reactive Variable
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 Html and JS for reference.
这里有Html和JS供参考。
<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');
}
#1
1
Edit of onCreated:
编辑onCreated:
Template.templateName.onCreated(function() {
this.variableName = new ReactiveVar(false);
});
You may want to add a helper function to grab the Template instance:
您可能需要添加辅助函数来获取Template实例:
Template.templateName.helpers({
getVariableName: function() {
return Template.instance().variableName.get();
}
});
and then you could potentially call within your form logic
然后你可以在你的表单逻辑中调用
AutoForm.hooks({
myForm: {
onSuccess: function(operation, result) {
Template.getVariableName.set(false);
},
}
});
The MeteorChef has a great article about reactive variables/dictionaries Reactive Variables.
MeteorChef有一篇关于反应变量/字典Reactive Variables的文章很棒。
#2
0
If people stumble upon this for a solution, based this thread I as able to access the parents Reactive Dict/Reactive Variable after installing aldeed:template-extension like so
如果人们偶然发现这个解决方案,基于这个线程,我可以在安装aldeed:template-extension之后访问父节点Reactive Dict / Reactive Variable
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 Html and JS for reference.
这里有Html和JS供参考。
<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');
}