将当前视图保存在变量中以便以后再渲染它

时间:2016-02-18 16:03:23

标签: meteor meteor-blaze

我正在复制我的问题的简化版,提出一个基本问题。

我有一个名为'form'的模板,

<template name="form">
    <input id="name" type="text">
    <button id="submitform type="submit">Submit</button>
</template>

我正在按如下方式处理提交事件,

Template.form.events({
    'click #submitform': function() {
        event.preventDefault();
        if(Meteor.user())
            alert('submitted');
        else{
            //save the filled form along with view (basically the template or view)
            Router.go('login');
        }
    }
});

我的问题是,如何使用填充在变量/任何方式中的数据保存填充的模板,以便我可以在登录后进行渲染。 这就是我渲染表单模板的方式,

Router.route('/form', function () {
  this.render('form');
});    

我想要的是能够在他登录时为他呈现用户填充的模板/视图。请告诉我是否有流星方式来做这件事,而不是简单的JavaScript破解。请询问您是否需要其他数据/代码。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你可以使用session变量。

<template name="form">
    <input id="name" type="text" value="{{name}}">
    <button id="submitform type="submit">Submit</button>
</template>

Template.form.helpers({
    name() {
        return Session.get('name');
    }
});

Template.form.events({
    'click #submitform': function(event, template) {
        event.preventDefault();
        if(Meteor.user())
            alert('submitted');
        else{
            Session.set('name', template.find('#name').value);
            Router.go('login');
        }
    }
});

然后在Meteor.loginWithPassword回调中重定向回表单路由。

答案 1 :(得分:0)

如果您想要保存包含用户数据的表单,您可以将其隐藏起来,然后在您想要显示它时再次显示它吗?

否则,您可以从save formData对象重新填充表单。见Use jquery to re-populate form with JSON data

相关问题