在数组上使用set() [英] Using set() on an array
问题描述
我有一个EmberArray缓存日常活动。因此,如果我从7月1日的服务器中提取了活动,则会在 activitiesByDay ['2013-07-01']
中找到。我的问题是如何获取()和set()这些数组项,以便我可以利用Ember的Run Loop和数据绑定属性。
I have an EmberArray that caches all activities at the day level. Therefore if I've pulled activities from the server for 1st July it will be found in activitiesByDay['2013-07-01']
. My problem is how to get() and set() these array items so that I can take advantage of Ember's Run Loop and data binding properties.
以下是完整的代码:
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;
});
}
正如你所看到的,这个订单项说...
As you can see, the line item which says ...
this.get('activitiesByDate')[params.on_date] =
this.get('activitiesByDate')[params.on_date] =
是一个问题,在不让Ember知道的情况下进行更改。问题是我找不到在数组上使用Ember的 set()
的方法,它的格式是YYYY-MM-DD格式的日期。我认为这个关键结构要求我使用Javascript的括号表示法和Ember应该处理的点符号。
is a problem because I'm making changes without letting Ember know. The problem is I can't find a way to use Ember's set()
on an array who's keys are dates of the format "YYYY-MM-DD". This key structure, I believe, requires me to use Javascript's bracket notation versus dot notation that Ember should handle.
----更新----
---- UPDATE ----
我已更新了 ActivitiesRoute
有一点,所以 activitiesByDate
是一个对象,而不是一个Ember.Array。我也改变了所有的setter / getter到这个对象的属性来使用Embers get / set()`
I have updated the ActivitiesRoute
a little so that the activitiesByDate
is an object rather than an Ember.Array. I have also changed all setters/getters to the properties of this object to use 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
似乎工作正常以下是三个不同日期之后状态的例子:
The activitiesByDate
object seems to work fine. Here's an example of what it's state looks like after having gone to three separate dates:
查看这些对象,我可以看到它们包含适当的活动量,使部分看起来不错。不幸的是,当我在浏览器中重新加载时,我的计算属性 - activitiesOnDate
不会被更新。
Looking inside these objects I can see that they contain the right amount of activities so that part looks good. Unfortunately my computed property -- activitiesOnDate
is not being updated outside of when I hit reload in the browser.
推荐答案
如果您使用 set('someProperty。'+ dynamicProperty,value)
和 get('someProperty。'+ dynamicProperty)
,你将拥有你想要的东西。在您的情况下:
If you use set('someProperty.' + dynamicProperty, value)
and get('someProperty.' + dynamicProperty)
, you will have what you want. In your case:
获取筛选结果
//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');
这篇关于在数组上使用set()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!