【问题标题】:Meteor Iron Router data from params on click点击时来自参数的 Meteor Iron Router 数据
【发布时间】:2014-05-01 00:38:08
【问题描述】:

我通过 Iron Router 将数据上下文传递到页面。我可以用 this._id 调用它。

但是,当您通过空格键在每个循环中的元素上有一个单击事件时,它会将“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...但正如你在这个例子中看到的那样,我不能。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 我想我可以将它设置为 Session 但我必须有另一种方式来访问点击事件中的模板数据上下文。

标签: javascript meteor iron-router


【解决方案1】:

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

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

【讨论】:

  • 已修复。对此感到抱歉。
  • 这在 Iron 中不起作用:路由器 1.0.12,我不得不使用 Router.current().params
【解决方案2】:

你也可以这样做:

假设您的页面位于 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。Iron Router 需要将内容作为哈希/键值传递。

然后在router.js中:

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

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2014-12-31
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多