重写jQuery函数以使用Meteor.js

时间:2015-07-04 03:40:20

标签: javascript jquery meteor

我写了一小段jQuery,它在Rails应用程序中运行得很好:

$(".myfilter li").click(function () {
    $("." + $(this).data('class')).toggle('slow')
    $(this).toggleClass("secondary")
});

我正在尝试使用Meteor JS重新编写功能。这是我正在尝试的,但它不起作用:

Template.postList.events({
  "click .myfilter li":function(event, template) {
    template.$("." + $(this).data('class')).toggle('slow');
    template.$(this).toggleClass("secondary");
  }
});

以下是HTML文件。

发表-list.html

<template name="postList">
<div class="row">
    <div class="large-12 columns">
        {{ >postForm }}
        <h2>Posts for {{name}}</h2>
        <ul class="button-group myfilter">
            {{ #each posts }}
              {{ >postTypes }}
            {{ /each }}
        </ul>
    </div>
</div>
</template>

发表-types.html

<template name="postTypes">
    <li><a href="#" class="button tiny">{{postType}}</a></li>
</template>

1 个答案:

答案 0 :(得分:1)

问题在于this

在你的jquery函数this绑定到选择器。在Meteor中,事件映射this绑定到模板或模板数据,具体取决于创建模板的上下文。

您需要将this替换为event.targettemplate.find()