我正在尝试通过Meteor更新KineticJS对象的位置。
问题出现在:
Players.update({name: "Rect"}, {xpos: this.attrs.x})
以下是流星文档的说法:
// Find the document with id "123", and completely replace it.
Users.update({_id: "123"}, {name: "Alice", friends: ["Bob"]});
我试图通过以下方式检查数据是否正在更新:
console.log(Players.findOne({name: "Rect"}).xpos);
这是github:
https://github.com/randompast/randomtests/tree/master/meteorCanvasTest
答案 0 :(得分:3)
首先,总是使用$ set来更新你的属性,以免踩到像名字这样的东西。由于您在后续更新中踩过了名称,因此没有更新名为“rect”的属性。 Players.update({name: "Rect"}, {$set: {xpos: this.attrs.x}})
if (Meteor.is_client) {
Players.find().observe({
changed: function(new_doc, idx, old_doc) {
if(MyTest.rect) {
MyTest.rect.attrs.x = new_doc.xpos;
MyTest.layer.draw();
}
}
});
....
MyTest.rect.on("dragend", function() {
Players.update({name: "Rect"}, {$set: {xpos: this.attrs.x}});
});
....
}
只需插入观察功能并确保您的dragend使用$ set符号。