【问题标题】:How to pass parameter data between templates in meteor flowlayout flowrouter如何在流星flowlayout flowrouter中的模板之间传递参数数据
【发布时间】:2016-01-01 14:04:02
【问题描述】:

我有一个名为:Orders 的模板,它显示了我的订单图像集合:

{{#each images}}
<div class="images">
  <a href="/order/{{this._id}}"> <img class="image" src="{{this.url  }}"  /></a>
</div>
{{/each}}

不,我希望另一个名为 order 的模板仅向我显示我单击的集合中的一个项目:我尝试这样做:1. 单击图像的 orders.js 事件:

"click .image": function() {
            Images.find({_id:this._id});

和orders.html:

<a href="/order/{{this._id}}"> <img class="image" src="{{this.url  }}"  /></a>

我也有 routes.js :

FlowRouter.route("/orders", {                 **this part works fine**
                    action: function(){
                    FlowLayout.render("layout",{top:"orders", main:"test"});
                    }

  FlowRouter.route('/order/', {             **How do I do this part ????????**
                    action: function(){
                    FlowLayout.render("layout",{top:"order",data:image});

                    }

我使用动态布局模板来显示显示良好的订单。 如何设置单序html、路由和渲染???

【问题讨论】:

    标签: meteor flowlayout


    【解决方案1】:

    回答你的问题:

    从你的路径开始,你应该像这样在路径中传递 id 参数:

    FlowRouter.route('/order/:imageId', {             
      action: function() {
        FlowLayout.render("layout",{ top: "order", main: "test" });
      }
    });
    

    查看包含模板订单的 order.html 文件的渲染,类似于:

    <template name="order">
      {{#with image}}
        <!-- whatever you want to do -->
        <img src="{{url}}" />
      {{/with}}
    </template>
    

    此模板使用 order.js,订阅您的收藏仅用于一张图像和一个名为 image 的助手,它查找您通过函数 FlowRouter.getParam 在路由中传输的参数 imageId

    Template.order.onCreated(function() {
        var imageId = FlowRouter.getParam('imagedId');
        if ( imageId !== undefined ) {
          Meteor.subscribe('oneImage', imageId);
        }      
    }
    
    Template.order.helpers({
      image: function() {
        var imageId = FlowRouter.getParam('imagedId');
        if ( imageId !== undefined ) {
          return Images.findOne({ _id: imageId });
        }
      }
    }
    

    最后,服务器端,你应该做一个发布:

    Meteor.publish('oneImage', function(imageId) {
      return Images.findOne({ _id: imageId });
    }
    

    按照这种方式,您不再需要您的活动click .image,并且您优化了您的表现! ;)

    顺便说一句,在您的 {{#each}} 循环中的 orders.html 上,您无需编写 {{this.url}}{{this._id}}{{url}}{{_id}} 都可以;)

    【讨论】:

    • 谢谢 Xavier,我会试试这个。
    【解决方案2】:

    要检索请求参数,您可以使用:

    FlowRouter.current().route._params.keys.id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 2014-08-22
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      相关资源
      最近更新 更多