我有一条使用星期几查找日期对象的路线
//assume I have a configuration model for 7 days (1 for each week day basically)
var day_of_week = ev.date.getDay() + 1;
var model = App.Day.find(day_of_week);
model.set('date', selected_date); //selected_date is mm/dd/yyyy (input from user)
router.transitionTo('day', model);
在该路由中,我重定向到另一个了解相关对象的路由
App.DayRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('appointments');
},
serialize: function(model) {
return {day_date: model.get('date')};
}
});
在该模板中,我每个对象都经过
{{#each appointment in controller}}
{{appointment}}<br />
{{/each}}
这是一个基本的计算属性
appointments: function() {
//do stuff in here
}.property()
我注意到,当我第一次加载时,我的计算属性(位于上面所示的堆栈的最底部)会像它应该的那样被调用。但是当我回到那一天时,我无法触发计算属性(假设因为它是一个已经加载/缓存在内存中的模型)。
我需要重新加载它,因为我确实更改了日期(即使对象的其余部分完全相同)。
当日期更改时,如何强制重新渲染/又称停止缓存的属性?我尝试修改属性以在日期更改时触发,但仍然没有骰子...
appointments: function() {
//do stuff in here
}.property('date')
更新 - 显示带有计算属性的完整模型
我的约会计算属性(如上所示)实际上卡在我的模型上(因为它是基于日期模型的配置)
App.Day = DS.Model.extend({
times: [],
first: attr('string'),
last: attr('string'),
interval: attr('number'),
day_of_week: attr('number'),
date: attr('string'),
listings: function() {
return App.Appointment.find(this);
}.property(),
slots: function() {
var slots = App.Slot.find(); //this returns configuration -not persisted data from $.ajax)
for(var i = 0; i < slots.get('length'); i++) {
if (slots.objectAt(i).get('day_of_week') === this.get('day_of_week')) {
var slot = slots.objectAt(i);
slot.set('day', this);
this.get('times').push(slot);
}
}
return this.get('times');
}.property(),
appointments: function() {
App.Appointment.clear(); //just resets this js array each time to get a fresh apt collection (again -because it's dynamic based on configuration)
var slots = this.get('slots');
var interval = this.get('interval');
for(var i = 0; i < slots.get('length'); i++) {
var entry = App.AppointmentFactory.create(slots.objectAt(i), interval);
App.Appointment.add(entry);
}
return this.get('listings');
}.property()
});
再次更新
看起来我忘记显示约会索引路线 - 非常重要,因为它显示了我如何填充给定日期模型的约会数组
App.AppointmentsIndexRoute = Ember.Route.extend({
model: function(params) {
var day = this.modelFor("day");
return day.get('appointments');
}
});
最佳答案
我认为你需要包括 @each
为此,如果事实上date
出现在每次约会中,并且您的计算属性位于 appointments
级别。
或者,您可以使用 .volatile()
来阻止缓存,但是正确定义的观察者不需要这样做。
关于ember.js - 如何使用 ember.js 中的缓存属性重新渲染模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16409228/