流星路由器强制重新渲染

时间:2015-06-30 09:57:26

标签: meteor iron-router

我有以下路线

/产品/ 123

/产品/ 456

模板呈现产品数据并允许用户拖动

当我重定向到另一个类似

的产品时
Router.go("product",{_id:"456"});

模板更新数据但不重新渲染html。这意味着用户拖动的内容保持不变。这对某些情况有好处,但不适合我的情况

对我有用的唯一解决方案是重定向到另一个设置清晰模板的页面,并将其重定向到产品页面

我的路由器功能:

Router.route('product/:_id/', {
    name:"product",
    data:function(){
      var data =  {product: Products.findOne({_id:objectId(this.params._id)})}
      return data;
    },
    waitOn:function(){
      return Meteor.subscribe('Products',this.params._id);
    },
    yieldTemplates: {'product': {to: 'mainArea'}},
});

我需要一种方法告诉路由器或模板重置html

1 个答案:

答案 0 :(得分:1)

One solution is to set up an autorun in the template's onRendered function that looks for changes to the URL parameters and the resets the template as needed. Something like this:

Template.myTemplate.onRendered(function() {

   var controller = Router.current()
   this.autorun(function() {
      var params = controller.getParams() // Reactive

      // Clear up your drag interface here
   });   
});

By accessing controller.getParams() (the route controllers reactive parameter list) the outrun will be called when you move between routes on the same template.

相关问题