简体   繁体   中英

Re-Writing jQuery Function to Work with Meteor.js

I wrote this small piece of jQuery that worked great in a Rails app:

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

I'm trying to re-write the functionality using Meteor JS. Here is what I'm trying, however, it is not working:

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

Here are the HTML files.

post-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>

post-types.html

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

The issue is with this .

In your jquery function this is bound to the selector. In Meteor event maps this is bound to the template, or the template data, depending on the context in which the template is created.

You will want to replace this with something like event.target or template.find() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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