ember.js - 如何使用 ember.js 中的缓存属性重新渲染模板?

标签 ember.js ember-data

我有一条使用星期几查找日期对象的路线

//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/

相关文章:

javascript - 在 Sails.js 上获取请求 Ember.js

jquery - 我想在使用 Ember.Js 单击导航时查看内容

java - 如何让 Java/Spring MVC 返回基于 jsonapi.org ID 的格式?

java - 如何正确格式化 Hibernate 实体以与 ember-data 一起使用?

ember.js - 根据用户权限显示/隐藏内容的最佳方法

testing - 我应该如何在 ember 中进行测试?

ember.js - ember.js 中的多个动态段

ember.js - Ember : How to cleanly replace model data and have progress indicators

javascript - 在 Ember 中返回模型属性的 promise

javascript - 加载路由 : TypeError: Cannot set property 'typeKey' of undefined 时出现 Ember Data 1.0 错误