我想插入一个Meteor模板(一个简单的登录表单),但我想控制表单提交后发生的事情.理想情况下,我希望能够将函数传递afterLogin()给模板.但是我很不确定如何做到这一点,如果这是可能的话.
我最近看到了一个有趣的包viewmodel,我不确定它是多么相关.但是,此上下文中的目标基本上是使用不同的视图模型呈现视图.
有任何想法吗?我目前使用一个会话变量,然后登录后,我检查会话变量运行正确的功能,但是这是丑陋和不容易的工作.有任何想法吗?
我是这样做的:
我假设您的登录表单是从父模板中调用的,使用attributes语法将自定义帮助程序的值传递给登录表单的数据上下文.
<template name="parent">
{{> loginForm options=loginFormOptions}}
</template>
Run Code Online (Sandbox Code Playgroud)
帮助器返回一个封装函数的对象,调用者负责将此函数设置为他们想要的任何东西.
Template.parent.helpers({
loginFormOptions:function(){
return {
afterLogin:function(){
// we assert that the context is correct :
// this will print Template.loginForm
console.log(this.view.name);
}
};
}
});
Run Code Online (Sandbox Code Playgroud)
您的登录表单代码充当库,可以从其数据上下文中读取调用者模板传递的函数,然后使用适当的this上下文调用该函数.
Template.loginForm.events({
"submit":function(event,template){
// ...
Meteor.loginWithPassword(...,function(error){
if(error){
console.log(error);
return;
}
// guard against the existence of the custom afterLogin function
if(template.data.options && template.data.options.afterLogin){
// execute the custom function with proper context
template.data.options.afterLogin.call(template);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
899 次 |
| 最近记录: |