0

我有一个 EmberArray 可以缓存白天的所有活动。因此,如果我从服务器中提取了 7 月 1 日的活动,它将在activitiesByDay['2013-07-01']. 我的问题是如何获取()和设置()这些数组项,以便我可以利用 Ember 的运行循环和数据绑定属性。

这是完整的代码:

module.exports = App.ActivitiesRoute = App.AuthenticatedRoute.extend({
    onDate: null,
    actionList: Ember.ArrayController.create(),
    activitiesByDate: Ember.Array,
    activitiesOnDate: function() {
        return this.get('activitiesByDate')[this.get('onDate')].content;
    }.property('activitiesByDate','onDate'),
    if (!this.get('activitiesByDate')[params.on_date]) {
        this.set('activities', this.store.find('activity', {on_date: params.on_date}));
        this.get('activitiesByDate')[params.on_date] =
            this.store.filter('activity',function(activity){        
                var itemDate = new Date(activity.get('start_time'));
                itemDate = moment(itemDate).format('YYYY-MM-DD');
                return itemDate === params.on_date;
            });
    }

如您所见,显示...的行项目

this.get('activitiesByDate')[params.on_date] =

是一个问题,因为我在不让 Ember 知道的情况下进行更改。问题是我找不到set()在数组上使用 Ember 的方法,数组的键是“YYYY-MM-DD”格式的日期。我相信,这个关键结构要求我使用 Javascript 的括号表示法与 Ember 应该处理的点表示法。

- - 更新 - -

我已经更新ActivitiesRoute了一点,以便它activitiesByDate是一个对象而不是 Ember.Array。我还将所有 setter/getter 更改为该对象的属性以使用 Embers get/set()`

App.ActivitiesRoute = App.AuthenticatedRoute.extend({
    onDate: null,
    activitiesByDate: Ember.Object.create(),
    activitiesOnDate: function() {
        return this.get('activitiesByDate.' + this.get('onDate'));
    }.property('activitiesByDate'),

    model: function(params) {
        this.set('onDate', params.on_date);
        // check if the given date has activities already loaded and if not then load it
        // if it does then do NOT reload. In the case of wanting a refresh use the refreshDate() method
        if (!this.get('activitiesByDate')[params.on_date]) {
            console.log('getting activities for: ' + params.on_date);
            this.set('activities', this.store.find('activity', {on_date: params.on_date})); // go get the dates from Server
            this.set('activitiesByDate.' + [params.on_date], this.store.filter('activity',function(activity){       
                var itemDate = new Date(activity.get('start_time'));
                itemDate = moment(itemDate).format('YYYY-MM-DD');
                return itemDate === params.on_date;
            }));
        }

        return true;
    },
    setupController: function(controller, model) {
        controller.set('activitiesByDate',this.get('activitiesByDate'));
    }

activitiesByDate对象似乎工作正常。下面是一个例子,说明它在三个不同的日期之后的状态:

查看这些对象的内部,我可以看到它们包含适量的活动,因此该部分看起来不错。不幸的activitiesOnDate是,当我在浏览器中点击重新加载时,我的计算属性没有被更新。

4

1 回答 1

1

如果你使用set('someProperty.' + dynamicProperty, value)and get('someProperty.' + dynamicProperty),你会得到你想要的。在你的情况下:

获取过滤结果

//Instead of
this.get('activitiesByDate')[this.get('onDate')]

// You would use 
this.get('activitiesByDate.' + this.get('onDate'));

设置过滤结果

// Instead of
var filtered = this.store.filter('activity',function(activity){        
    var itemDate = new Date(activity.get('start_time'));
    itemDate = moment(itemDate).format('YYYY-MM-DD');
    return itemDate === params.on_date;
});

this.get('activitiesByDate')[params.on_date] = filtered;


// You would use 
var filtered = this.store.filter('activity',function(activity){        
    var itemDate = new Date(activity.get('start_time'));
    itemDate = moment(itemDate).format('YYYY-MM-DD');
    return itemDate === params.on_date;
});

// if some key change, notify the entire property
Ember.propertyWillChange(this, 'activitiesByDate');
this.set('activitiesByDate.' + params.on_date, filtered);
Ember.propertyDidChange(this, 'activitiesByDate');
于 2013-10-07T23:01:52.447 回答