Javascript 为什么不是';t流星收集是否有反应?
TLDR:我想跟踪Meteor集合的依赖项,以找出我的模板助手为什么不是被动的。 我一直在尝试在Meteor中创建一个可在不同模板中重用的反应式清单组件Javascript 为什么不是';t流星收集是否有反应?,javascript,meteor,reactive-programming,Javascript,Meteor,Reactive Programming,TLDR:我想跟踪Meteor集合的依赖项,以找出我的模板助手为什么不是被动的。 我一直在尝试在Meteor中创建一个可在不同模板中重用的反应式清单组件 模板: <template name="checklist"> <ul> {{#each items}} <li> <label> <input type="checkbox" value="
<template name="checklist">
<ul>
{{#each items}}
<li>
<label>
<input
type="checkbox"
value="{{value}}"
checked="{{isChecked}}"
data-id="{{_id}}"
/> {{name}}
</label>
<span> Status: {{status}}</span>
</li>
{{/each}}
</ul>
{{checkedIds}}
</template>
if (Meteor.isClient) {
var list;
/**
*
* Creates a Checklist instance, with a local collection that maintains the status
* of all checkboxes: 'checked', 'unchecked' or 'indeterminate'
*
*/
function createChecklist() {
var _checked = new Meteor.Collection(null),
check = function(id) {
return _checked.upsert({_id: id}, {_id: id, status: 'checked'});
},
getStatus = function(id) {
var item = _checked.findOne({_id: id})
return item && item.status;
},
isChecked = function(id) {
return _checked.find({_id: id, status: 'checked'}).count() > 0;
},
getCheckedIds = function() {
return _checked.find({status: 'checked'}).map(function(doc){return doc._id});
},
toggle = function(id) {
if ( isChecked(id) )
return uncheck(id);
else
return check(id);
},
uncheck = function(id) {
return _checked.upsert({_id: id}, {_id: id, status: 'unchecked'});
};
return Object.freeze({
'check': check,
'getCheckedIds': getCheckedIds,
'getStatus': getStatus,
'isChecked': isChecked,
'toggle': toggle,
'uncheck': uncheck
});
}
Template.checklist.helpers({
items: [
{_id: 0, name: 'Item 1', value: 10},
{_id: 1, name: 'Item 2', value: 20},
{_id: 2, name: 'Item 3', value: 40},
{_id: 3, name: 'Item 4', value: 20},
{_id: 4, name: 'Item 5', value: 100},
],
isChecked: function() {
return list.isChecked(this._id);
},
status: function() {
return list.getStatus(this._id);
},
checkedIds: function() {
return EJSON.stringify(list.getCheckedIds());
}
});
Template.checklist.events({
'change [type=checkbox]': function(e, tmpl) {
var id = e.target.dataset.id;
list.toggle(id);
}
});
Template.checklist.created = function() {
list = createChecklist();
}
}
您会注意到,checkedds
helper会在您选中某个框时进行反应性更新。但是,状态
帮助程序不会进行反应性更新
我正在努力:
\u checked
集合的依赖项,以确定是否已将状态
辅助对象添加为计算Deps.active=true
状态
帮助程序中,以检查我勾选复选框时它是否无效(它从不无效):
var comp=Deps.currentcompulation;
组件onInvalidate(函数(){
console.track();
});代码>
\u id
作为字符串存储在Mongo中
改为:
getStatus = function(id) {
var item = _checked.findOne({_id: String(id)})
return item && item.status;
},
哈哈,一位同事给我看了这个,我正在回答我自己的问题,突然你的答案出现了。谢谢你!