如何在meteor flowlayout flowrouter中的模板之间传递参数数据

时间:2016-01-01 14:04:02

标签: meteor flowlayout

我有一个名为的模板: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,路由和渲染????

2 个答案:

答案 0 :(得分:0)

回答你的问题:

从您的路线开始,您应该在路径中传递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的帮助程序,通过函数{{1}查找您在路径中传输的参数imageId }:

FlowRouter.getParam

总而言之,服务器方面,你应该做一个出版物:

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循环中的orders.html,您不需要写{{#each}}也不需要{{this.url}}{{this._id}}{{url}}很好;)

答案 1 :(得分:0)

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

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