我在流星,並已在我的項目中使用Reactive Var,沒有任何問題。在當前的例子中,我不確定是否需要使用ReactiveDict,因爲我沒有與ReactiveVar進行反應。何時使用ReactiveDict vs ReactiveVar
我將reactiveVar的值設置爲導致產生對象數組的mongo查詢。在陣列中
Template.MasterList.onCreated(function() {
this.teacherList = new ReactiveVar('');
});
Template.MasterList.onRendered(function() {
var weekNum = Session.get('CurrentWeek').substr(0, 1);
var week = 'Week' + weekNum;
var query1 = {};
query1[week] = { $in: ['JC', 'JF', 'F']};
this.teacherList.set(Programs.find({ $and: [{ CampYear: Session.get('GlobalCurrentCampYear') }, query1]}, { sort: { FullName: 1 }}).fetch());
});
一個物體看起來像:
{ ...
Students: {
Week1: {
Monday: ['Joe', 'Mary']
},
Week2: {},
Week3: {}
}
...
}
一個數組中的對象可能會更改爲類似:
{ ...
Students: {
Week1: {
Monday: ['Joe', 'Mary', 'Bill']
},
Week2: {},
Week3: {}
}
...
}
當變化的一個對象發生時,reactiveVar不會看到數組中的更改以重新激發幫助程序。是否在此使用ReactiveDict?
編輯:
輔助功能:
saturdayTeacher: function (name) {
var weekNum = Session.get('CurrentWeek').substr(0, 1);
var week = 'Week' + weekNum;
var teachers = Template.instance().teacherList.get();
for (var i = 0, len = teachers.length; i < len; i++) {
if (_.contains(teachers[i].Students[week].Saturday, name)) {
Meteor.defer(function() {
i = 0;
});
var teacher = teachers[i].FullName.split(' ');
return teacher[0] + " " + teacher[1].slice(0, 1);
}
}
},
這可能有幫助https://stackoverflow.com/questions/33510951/meteor-reactivevar-vs-reactivedict –