Template.foo.rendered适用于所有' foo'

时间:2015-03-17 16:52:55

标签: javascript css meteor css-selectors

我目前正在投票论坛。

如果我进入帖子,它会是这样的。

  • 邮报
    • 答案
    • 答案
    • 答案
    • 等......

我有方法在点击upvote和downvote,undownvote和unvote时调用meteor.methods。它将用户插入downvoters,upvoters,inc或inc -1票等。

然后我有以下代码来禁用相反的按钮,如果点击。

例如,单击upvote时,将禁用downvote。单击downvote时,upvote被禁用。

Template.answerItem.rendered

Template.answerItem.rendered = function() {
    if( $('#upvote').hasClass('unvote') ) {
        $('#downvote').attr('disabled', 'disabled');
    }

    if( $('#downvote').hasClass('undownvote')) {
        $('#upvote').attr('disabled', 'disabled');
    }
}

问题在于,如果我在一个帖子中有多个答案,这将适用于所有的answerItems,而不仅仅是它所应用的一个answerItem。

例如,如果我点击upvote中的answer1,则downvoteanswer1都会停用answer2

答案按如下方式加载

在帖子中(帖子)

{{#each answers}}
    {{> answerItem}}
{{/each}}

我如何才能使代码我只应用于(one)answerItem,以便它为每个answerItem分别运行?

EDIT1

更新信息(代码)

Template.answerItem的HTML投票片段

<div class = "voting-container">
    <button id="upvote" class="upvote {{upvotedClass}}">
        <span class="glyphicon glyphicon-triangle-top"></span>
    </button>

    <div class = "voteCount-container"> 
        <span>{{votes}}</span>  <!--To retrieve votecount from answers collection-->
    </div>

   <button id="downvote" class="downvote {{downvotedClass}}">
        <span class="glyphicon glyphicon-triangle-bottom"></span>
    </button>               
</div>

Template.answerItem.rendered

Template.answerItem.rendered = function() {
    var $downvote = this.$('.downvote');
    var $upvote = this.$('.upvote');

    if($upvote.hasClass('unvote'))
        $downvote.attr('disabled', 'disabled');

    if($downvote.hasClass('undownvote'))
        $upvote.attr('disabled', 'disabled');
}

Template.answerItem.helpers

Template.answerItem.helpers({

    upvotedClass: function() {
        var userId = Meteor.userId();
        if (userId && !_.include(this.upvoters, userId)) {
            return 'upvotable';
        }  else {
            return 'unvote';
        }    
    },
    downvotedClass: function() {
        var userId = Meteor.userId();
        if (userId && !_.include(this.downvoters, userId)) {
            return 'downvotable';
        } else {
            return 'undownvote';
        }
    }

});

Template.answerItem.events

Template.answerItem.events({


    'click .upvotable': function(e) {
    e.preventDefault();
    $('.downvotable').prop('disabled', true);
    Meteor.call('upvoteAnswer', this._id);
    },

    'click .unvote': function(e) {
    e.preventDefault();
    $('.downvotable').prop('disabled', false);
    Meteor.call('unvoteAnswer', this._id);
    },

    'click .downvotable': function(e) {
    e.preventDefault();
    $('.upvotable').prop('disabled', true);
    Meteor.call('downvoteAnswer', this._id);
    },

    'click .undownvote': function(e) {
    e.preventDefault();
    $('.upvotable').prop('disabled', false);
    Meteor.call('undownvoteAnswer', this._id);
    }

})

2 个答案:

答案 0 :(得分:1)

您可以使用this,然后使用parent()siblings()获取相关的.downvote元素,前提是

Template.answerItem.rendered = function() {
    if( $('.upvote').hasClass('unvote') ) {
        $(this).siblings('.downvote').attr('disabled', 'disabled');
    }

    if( $('.downvote').hasClass('undownvote')) {
        $(this).siblings('.upvote').attr('disabled', 'disabled');
    }
}

答案 1 :(得分:1)

HTML要求您不要在页面上重复相同的ID。由于每个answerItem都有一个#upvote,因此您最终会同时渲染多个。{/ p>

第1步

upvotedownvote ID替换为类。

第2步

您可以使用template.$将jQuery选择器仅隔离到当前模板中的元素。从rendered回调中,您可以使用this.$,如下所示:

Template.answerItem.rendered = function() {
  var $downvote = this.$('.downvote');
  var $upvote = this.$('.upvote');

  if($upvote.hasClass('unvote'))
    $downvote.prop('disabled', true);

  if($downvote.hasClass('undownvote'))
    $upvote.prop('disabled', true);
}

另请注意,.prop('disabled', true)显然是correct way to disable an input