我正在自动订阅客户端上的集合。我的代码如下。我希望客户有更新的信息,但它永远不会更新。这个问题表现为一个按钮,除非我刷新页面,否则永远不会被消息替换。
实际上,我为“测试”集合拥有的一个文档应该在客户端将其 testCount 属性更新为 1,但除非我刷新页面,否则它永远不会发生。不应该更新吗?
未安装自动发布包。
在这一点上,我确信 Meteor 中存在错误,或者我只是不了解基本的东西。我即将安装meteor.js 的开发版本并弄清楚它或者干脆放弃,直到Meteor.js 可能真正起作用。:P
我的控制台日志:
Tests updated!
checking test
no test
Tests updated! Object {_id: "ea9f6002-74c7-4f37-9f10-0167b3b6f65a", testCount: 0}
checking test result
test.testCount 0
我的服务器日志:
$ meteor reset;meteor
Project reset.
[[[[[ ~/Dropbox/projects/sphela-game/sphela ]]]]]
Running on: http://localhost:3000/
inserting new test
Connecting test, getting initial data.
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Connecting test, getting initial data.
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Running test.
Updating test to 1.
Test ran. { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Test added do nothing { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
我的html:
<head> <title>Testing counts.</title> </head>
<body> {{> app}} </body>
<template name="app">
{{#if testSuccess}}
<h1>Test complete.</h1>
{{else}}
<button class="btn run-test">Run Test</button>
{{/if}}
</template>
我的 JavaScript:
var Tests = new Meteor.Collection('tests');
if (Meteor.isClient) {
Meteor.startup(function() {
Session.set('testCount', 0)
Meteor.subscribe('connect');
});
Template.app.testSuccess = function() {
var test;
console.log('checking test result');
test = Tests.findOne();
if (!test) {
console.log('no test', test);
return false;
}
console.log('test.testCount', test.testCount);
return test.testCount > 0;
};
Template.app.events({
'click .run-test': runTest
});
function runTest(event) {
Meteor.call('runTest');
Session.set('testCount', 1);
}
Meteor.autorun(function() {
console.log('Tests updated!', Tests.findOne());
});
Meteor.autosubscribe(function() {
Meteor.subscribe('test-results', Session.get('testCount'));
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
test = Tests.findOne({})
if (!test) {
test = {
testCount: 0
};
console.log('inserting new test');
test._id = Tests.insert(test);
} else {
console.log('startup reset');
test.testCount = 0;
Tests.update({_id:test._id}, test);
}
});
Meteor.publish('connect', function() {
var test_;
console.log('Connecting test, getting initial data.');
test_ = Tests.findOne({});
this.set('tests', test_._id, test_);
this.complete();
this.flush();
});
Meteor.publish('test-results', function(test) {
var handle;
handle = Tests.find({testCount: test}).observe({
changed: _.bind(function(test) {
console.log('Test changed', test._id, test.testCount);
this.set('tests', test._id, test);
this.flush();
}, this),
added: _.bind(function(test) {
console.log('Test added do nothing', test);
this.flush();
}, this)
});
this.complete();
this.flush();
this.onStop(function() {
handle.stop();
});
});
Meteor.methods({
runTest: function() {
var test;
console.log('Running test.');
test = Tests.findOne({});
test.testCount = 1;
console.log('Updating test to 1.');
Tests.update({_id: test._id}, test);
console.log('Test ran.', Tests.findOne());
},
});
}