【问题标题】:How to send some data of one template to another template Meteor如何将一个模板的一些数据发送到另一个模板 Meteor
【发布时间】:2016-10-22 14:53:44
【问题描述】:

我想使用 Meteor 将一些数据从模板共享到另一个模板。我有一个模板,即 allInventory.html,我在其上以表格形式显示一些数据,我在那里添加了三个链接。一个用于查看、编辑和删除我想要的内容,我将所有数据从后端获取到帮助程序之一,即 productDetails,我将一个事件与查看按钮绑定,该按钮将获取当前用户点击哪个产品的数据,因此我已成功获取数据在我的 allinventory 模板中,但还有另一个模板,即我想在其上呈现或显示该数据的 productDetails。但坚持认为我有关于 allInventory 点击事件的数据,但不知道如何与 productDetails 模板共享相同的数据。

这是我的 allInventory.js

 Template.allInventory.rendered = function() {
Template.allInventory.events({
      "click .btn":function (e){
       data  = $(e.target).attr('data');
    Router.go('productDetail', {data: $(e.target).attr('data')}, {query: 'q=s', hash: 'hashFrag'});
    console.log("button clicked.."+data);
    console.log(data);

  }
})

ProductDetails.js

Template.productDetail.rendered = function () {
Template.productDetail.helpers({

 productDetails: function() {  
      return data;
}
    });

allInventory.html

<button type="button" data ="{{productInfo}}" class="btn btn-info btn-sm"><i class="fa fa-eye"></i>View</button>

我只是想与 productsDetails 模板共享 allInventory 模板数据。

如有任何帮助,我们将不胜感激! 谢谢

【问题讨论】:

    标签: node.js templates meteor meteor-blaze


    【解决方案1】:

    如果要在模板之间共享数据,可以使用 Session 变量。

    您可以按照本指南进行操作:

    http://meteortips.com/first-meteor-tutorial/sessions/

    【讨论】:

      【解决方案2】:

      为此我建议避免使用 Session,因为它是一个全局对象,但更重要的是,因为有更好的方法来做到这一点。

      您可以使用帮助器将数据从父模板传递到子模板:https://guide.meteor.com/blaze.html#passing-template-content

      您可以使用回调 https://guide.meteor.com/blaze.html#pass-callbacks 将数据从子模板传递到父模板

      我会将此应用程序构建为具有容器(页面)模板,该模板将包含所有订阅并根据 URL 呈现您的模板之一。

      【讨论】:

        【解决方案3】:

        我会将这两个模板放在第三个父模板中。

         ParentTemplate
          -SharedInfo
          -KidTemplate1
          -KidTemplate2
        

        然后让第三个模板保存您想要跨模板共享的信息。

        为此,您可以使用 ReactiveVar,确保父模板上由 template1 代码进行的更改在 template2 中也是可见的。

        要访问孩子的父模板,您可以执行以下操作:

        Blaze.TemplateInstance.prototype.parentTemplate = function (levels) {
        var view = Blaze.currentView;
        if (typeof levels === "undefined") {
            levels = 1;
        }
        while (view) {
            if (view.name.substring(0, 9) === "Template." && !(levels--)) {
                return view.templateInstance();
            }
            view = view.parentView;
        }
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-04
          相关资源
          最近更新 更多