为什么不是流星收藏行为反应?
问题描述:
TLDR:我想跟踪Meteor Collection的依赖关系,找出为什么我的模板帮助程序没有反应。为什么不是流星收藏行为反应?
我一直在尝试在Meteor中创建一个可反复使用的清单组件,该组件可以在不同的模板中重用。
模板:
<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>
的Javascript:
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();
}
}
您会注意到,checkedIds
助手每当您选中一个框时都会进行被动更新。但status
帮助程序未被动更新。
我想:
轨道的的
_checked
收集的依赖关系,以制定出如果status
助手已添加的计算。了解为什么这个帮手没有被动更新。
如果任何人都可以帮助这些项目,我会很感激。
到目前为止,我已经做了以下内容:
- 证实
Deps.active = true
状态帮手内(其功能调用) -
将下面的代码
status
帮手内部检查,如果它是无效时我勾选复选框(这是从来没有失效):var comp = Deps.currentComputation; comp.onInvalidate(function() { console.track(); });
答
_id
在Mongo中作为字符串存储。
更改为:
getStatus = function(id) {
var item = _checked.findOne({_id: String(id)})
return item && item.status;
},
哈哈,我只是一个工作的同事表示这并回答我的问题时,你的答案弹出。感谢堆! – francisbyrne 2014-09-24 09:04:52