【发布时间】:2019-03-23 23:42:52
【问题描述】:
有很多代码,所以人们可以准确地理解我的意思。我正在开发一个流星应用程序,我在 Mongo DB 数据库中遇到了一些非常奇怪的行为,所以我编写了这个简单的测试程序来找出发生了什么。我观察到的行为是,当您在模板外部和内部引用 Meteor/Mongo DB 集合时,您会得到不同的结果。如果您调用函数将其插入模板之外的集合中,则只能在页面重新加载之前找到 DB 行。当从源自模板内部的调用插入行时,它们会持续存在,但不能被模板外部的查找代码引用。要复制它,您可以加载下面的程序。在页面加载时它会提醒您 1。如果您单击插入 3 次并单击计数,您将看到 4。如果您重新加载,您将收到 1 的警报,然后如果您单击计数,它将显示 5。如果这是已知和设计的行为是否有任何技巧可以绕过它,或者在使用流星时你真的必须对所有东西都使用模板吗?
测试.HTML
<head>
<title>Templates</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h1>Hello World!</h1>
{{greeting}}
<input type="button" value="Insert" class="insert"/>
<input type="button" value="Count" class="count"/>
<input type="button" value="Remove All" class="remove"/>
</template>
还有 test.js
Test = new Meteor.Collection("test");
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to test";
};
Template.hello.events({
'click input.insert': function () {
// template data, if any, is available in 'this'
Test.insert({testid:"test"});
},
'click input.count': function () {
alert(Test.find().count());
},
'click input.remove': function () {
var cursor = Test.find();
cursor.fetch().forEach(function(test){
Test.remove(test._id);
});
}
});
Test.insert({test:"test"});
var testCursor = Test.find();
alert(testCursor.count());
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
【问题讨论】:
标签: meteor