爲什麼這不是反應?更重要的是,如何才能做出反應?爲什麼這個模板沒有反應?
我想要將數據保存在Mongo中並在模板中使用。我可以使用ReactiveVar或ReactiveDict。我需要兩份數據嗎?
是不是Suspects.findOne('bruce')
已經返回一個被動對象?我試圖直接在Bruce上提供human
答案,但沒有觸發更新。
事件fire,log(this)顯示bruce的答案已更改,但該模板未重新呈現。有什麼好辦法做到這一點?
http://meteorpad.com/pad/KoH5Qu7Fg3osMQ79e/Classification
這是流星1.2 iron:router
補充說:
<head>
<title>test</title>
</head>
<template name="question">
{{#unless isAnswered 'human'}} <!-- :-< I'm not reacting here -->
<div>Sir, are you classified as human?</div>
<button id="no">No, I am a meat popsicle</button>
<button id="smokeYou">Smoke you</button>
{{else}}
<div> Classified as human? <b>{{answers.human}}</b></div>
{{/unless}}
</template>
和JavaScript:
// Why isn't this reactive?
if (Meteor.isClient) {
Template.question.helpers({
isAnswered: function (question) { // :-< I'm not reactive
var suspect = Template.instance().data;
return (typeof suspect.answers[question] !== 'undefined');
}
});
Template.question.events({
'click #no': function() {
this.answers.human = "No"; // :-< I'm not reactive
console.log(this);
},
'click #smokeYou': function() {
this.answers.human = "Ouch"; // :-< I'm not reactive
console.log(this);
}
});
}
// Collection
Suspects = new Meteor.Collection('suspects');
if (Meteor.isServer) {
Meteor.startup(function() {
// code to run on server at startup
Suspects.upsert('bruce', { quest: 'for some elements', answers: {}});
});
Meteor.publish('suspects', function() {
return Suspects.find({});
});
}
// Iron Router
Router.route('/', {
template: 'question',
waitOn: function() {
return Meteor.subscribe('suspects');
},
data: function() {
return Suspects.findOne('bruce');
}
});
感謝:-)
謝謝! fyi heres a [working meteorpad](http://meteorpad.com/pad/jfS9L85ikJpmDQYec/Classification%20(已解決))。 –