【问题标题】:Save the current view in variable to be able to render it later将当前视图保存在变量中以便以后渲染
【发布时间】:2016-05-30 21:53:00
【问题描述】:

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

我有一个名为“表单”的模板,

<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 hack。请询问您是否需要其他数据/代码。 提前致谢。

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    如果你想保存包含用户数据的表单,你能不能让它不可见,然后当你想显示它时再次可见?

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

    【讨论】:

      【解决方案2】:

      如果我理解您的问题,您可以为此使用 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 回调上重定向回表单路由。

      【讨论】:

      • 我正在寻找一种将整个渲染模板存储为变量的方法,这样我就可以使用 Blaze.render 渲染它。
      猜你喜欢
      • 2021-06-26
      • 2019-10-26
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2019-06-12
      相关资源
      最近更新 更多