【发布时间】:2015-10-05 18:31:33
【问题描述】:
我有
Template.templateName.onCreated(function() {
this.variableName = new ReactiveVar;
this.variableName.set(true);
});
在templateName 我有一个autoform。我需要在提交autoform 时将反应变量variableName 设置为false。
我试过了
AutoForm.hooks({
myForm: {
onSuccess: function(operation, result) {
this.variableName.set(false);
},
}
});
但它不起作用,因为 this. 不像在助手和事件中那样引用模板 templateName。如果我使用会话代替它会起作用,因为这些不限于特定模板/范围。
我可以做些什么来更改自动表单挂钩中的反应变量?
我也试过
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);
},
}
});
当使用console.log(this.template) 时,它会打印一个对象。如果我使用console.log(this.template.data) 我会得到
Object {id: "myForm", collection: "Meteor.users", type: "update", doc: Object, validation: "submitThenKeyup"…}
我使用反应变量variableName 来确定是显示可编辑表单还是为用户提供漂亮的数据展示。也许还有另一种更好的方法来做到这一点。
【问题讨论】:
-
我遇到了同样的情况,但我使用
Session和autorun解决了它。并且onDestroyed该模板我将Session设为null...等待知道任何其他更好的方法来解决这个问题...赞一个! -
@Jamgreen,
Template.templateName.onCreated和AutoForm.hooks代码是否在同一个文件中? -
你试过在你的钩子里使用
Template.instance().variableName吗? -
还有
console.log(Template.instance());打印null。
标签: javascript node.js meteor meteor-autoform