在下面的代码中,我试图把我的头放在这一行代码上。
Resolutions.update(this._id, {$set:{checked: !this.checked}});
这里是我收集的,请纠正我;
update -是对Mongodb集合调用的方法。
this._id - 更新的选择条件。
这个-是Template.body.helpers上下文。
_id -是mongodb中的项。
{$set:{checked:!this.checked}- 更新参数
$set: -是mongodb运算符
检查了-如果不存在要更新或创建的字段。
!this.checked --这是可行的,但我希望它是!this._id.checked,以否定checked的值。
谢谢
Resolutions = new Mongo.Collection('resolutions');
if (Meteor.isClient) {
Template.body.helpers({
resolutions: function() {
return Resolutions.find();
} });
Template.body.events({
'submit .new-resolution': function(event) {
var title = event.target.title.value;
Resolutions.insert({
title: title, createdAt: new Date()
});
event.target.title.value = "";
return false;
}
});
//object
Template.resolution.events({
//property1
'click .toggle-checked': function(){
//call update method on Mongo object
Resolutions.update(this._id, {$set:{checked: !this.checked}});
},
//property2
'click .delete': function(){
Resolutions.remove(this._id);
}
})
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}<head>
<title>resolutions</title>
</head>
<body>
<form class="new-resolution">
<input type="text" name="title" placeholder="a new customer">
<input type="submit" value="Submit">
</form>
<ul>
{{#each resolutions}}
{{> resolution}}
{{/each}}
</ul>
</body>
<template name="resolution">
<li>
<input class="toggle-checked" type="checkbox" checked="{{checked}}">
{{title}}
<button class="delete">Remove</button>
</li>
</template>
发布于 2016-01-31 23:22:32
在本例中,this实际上将是来自MongoDB的分辨率对象之一。您的助手说返回所有分辨率对象,您的模板要求为每个解析对象呈现一个解析模板。因此,获取this上下文的示例对象可能是:
{
"_id": "abcdefg",
"title": "Sample Title",
"checked": true
}所以当你在做更新的时候,你用英语说的是:
Resolutions.update(this._id, ...) -更新分辨率集合中的文档,其中内部id (_id)等于this._id。在我的例子中,这将是"abcdefg“。下一部分将描述我们将如何实际更新文档。
{$set:{checked: !this.checked}} --将决议集合中这些文档的值设置为使选中的属性与this.checked相反。在上面的示例中,我们将值设置为false。
您不需要this._id.checked的原因是因为您的对象是如何构造的,正如我上面所描述的-- this._id.checked将是未定义的。
https://stackoverflow.com/questions/35120631
复制相似问题