繁体   English   中英

单击时来自参数的Meteor Iron Router数据

[英]Meteor Iron Router data from params on click

我正在通过Iron Router将数据上下文传递给页面。 我可以用this._id来称呼它。

但是,当您通过空格键在每个循环中的元素上具有click事件时,它将“ this”更改为我刚刚单击的对象。 完全有道理。

但是,如何从刚刚单击的元素中获取一些属性,又如何从路由器中定义我的参数信息?

例如,如果我有一个帖子列表:

{{#each posts}}
  <li class="post-title">{{title}}</li>
{{/each}}

然后单击列出的那些元素:

'click .post-title': function(){
  console.log(this) //this will be the _id of the post I clicked.

 var new_object = {
   title: this.title,  //from that object I want that title
   page_url: // But here I want the params data I defined in the router //
 }
}

如果不是单击事件,我只能说this._id ...,但是正如您在这种情况下看到的那样,我做不到。 任何帮助,将不胜感激。 谢谢。

您可以像这样访问当前路线的数据:

'click .post-title': function(){
  var data = Router.current().data();
  console.log(data);
}

您也可以这样操作:

假设您的页面位于client / views / posts / post_item.html中,并且其中包含:

<div class="post-title">
  Something To Click On
</div>

您的JS文件将位于client / views / posts / post_item.js中并在其中:

 'click .post-title': function() {
    Router.go('postEdit', {_id: this._id});
  }

{_id: this._id}部分是如何将单击.post-title的帖子的_id传递给Iron Router的{_id: this._id} Router需要将内容作为哈希值/键值传递。

然后在router.js中:

  this.route('postEdit', {
    path: '/posts/:_id/edit',
    data: function() { return Posts.findOne(this.params._id); }
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM