Template.foo.renderd适用于所有'foo'
Template.foo.rendered applying to all 'foo'
我目前正在制作一个有投票的论坛。
如果我进入一个帖子,它看起来像这样。
- 《华盛顿邮报》
- 答
- 答
- 答
- 等等...
我有方法在点击赞成和反对、未反对和不投票时调用流星方法。它将用户插入到反对者,赞成者,inc或inc -1票等。
然后我有以下代码来禁用相反的按钮(如果单击)。
例如,单击"赞成票"时,将禁用"反对票"。单击"反对票"时,将禁用"赞成票"。
模板.答案项.渲染
Template.answerItem.rendered = function() {
if( $('#upvote').hasClass('unvote') ) {
$('#downvote').attr('disabled', 'disabled');
}
if( $('#downvote').hasClass('undownvote')) {
$('#upvote').attr('disabled', 'disabled');
}
}
问题是,如果我在一个线程中有多个答案,这将应用于所有答案项,而不仅仅是已应用于的一个答案项。
例如,如果我在 answer1
中单击upvote
,downvote
将在answer1
和answer2
中禁用。
答案加载如下
在线程(帖子)内
{{#each answers}}
{{> answerItem}}
{{/each}}
我将如何使我拥有的代码仅适用于(一个)answerItem,以便它为每个answerItem
单独运行?
编辑1
更新信息(代码)
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 = 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({
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({
'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);
}
})
HTML要求您不要在页面上重复相同的ID。因为每个answerItem
都有一个#upvote
,所以你最终会同时渲染多个。
步骤 1
将upvote
和downvote
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)
显然是禁用输入的正确方法。
您可以使用
this
,然后使用parent()
或siblings()
获取相关的.downvote
元素,前提是它们确实是兄弟姐妹。此外,将您的 ID 替换为类。
Template.answerItem.rendered = function() {
if( $('.upvote').hasClass('unvote') ) {
$(this).siblings('.downvote').attr('disabled', 'disabled');
}
if( $('.downvote').hasClass('undownvote')) {
$(this).siblings('.upvote').attr('disabled', 'disabled');
}
}
相关文章:
- if(foo!==null)的计算结果为true,即使foo为null
- 渲染<选项值=“;foo”;选择>在服务器上
- 代码挑战:创建一个跟踪对象实例总数的类Foo
- 为什么“foo()”在这里有效
- Template.foo.renderd适用于所有'foo'
- 调用 foo() 后的 x 和 y 值是什么
- 如何修复JSlint报告的“foo未定义”错误
- “this.foo”总是与“foo.bind(this)”相同吗?
- JavaScript 错误:未捕获类型错误:foo 不是一个函数
- 在这种情况下,“foo[x]”是什么意思?
- $("'" + foo + "'") vs $(foo)
- 为什么Object.protype.toString.call(foo)可以检测到foo's类型
- JavaScript美学:;函数foo(){}"vs“;var foo=函数(){}"AMD功能
- 在第'www.foo.com',可以从'www.example.com'将ajax请求发
- document.getElementById(foo).value,带有html编码的特殊字符
- jQuery"return{foo:bar,foo2:bar2}"-它是什么
- 如何在Javascript/AngularJS中动态地将foo设置为Object Property Name
- 使用$('#foo').val()==$('!#foo'!).is(':checke
- javascript getElementsByClassName("foo"||"bar
- 当用“”声明时,如何获得当前运行的函数名称;foo=函数(){..}”;