Emberjs-1.0.0-rc.6 使用 enumerable 列出特定日期发生的事件 [英] Emberjs-1.0.0-rc.6 using enumerable to list events occurring on a particular date

查看:16
本文介绍了Emberjs-1.0.0-rc.6 使用 enumerable 列出特定日期发生的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我定义一个控制器动作来显示发生在特定日期的日期时,它可以正常工作,但是如果我将该控制器动作转换为一个属性,它会停止显示发生在特定事件上的日期.jsfiddle

When I define a controller action to display dates occuring a particular date, it works correctly, but If I convert that controller action to a property it stops displaying the date occuring on a particular event. The jsfiddle

 App.EventsController = Em.ArrayController.extend({
   todayEvent: function(date){
     return this.get('content').filter(function(event) {
       return (moment(event.get('start')).unix() == moment(date).unix());
     });
   }
});

我可以获取控制器的实例:

I can fetch an instance of the controller:

 u = App.__container__.lookup("controller:events")

在25日的活动中,有2个活动,我可以用

on the event 25th, there are 2 events and I can fetch it with

u.todayEvent(new Date('2013-07-25').toString())

哪个正确返回

[> Class,  > class]

但在 CalendarEvent 控制器中,我想像上面一样显示特定日期的事件,但这次使用计算属性,所以我将 todayEvent 重新定义为计算属性,如下所示,仅此时间,它只返回 true 或 false 而不是返回包含当天事件的类对象.

But in the CalendarEvent controller, I want to display the events for a particular date just like above but this time using computed-property, so I redefine todayEvent asa computed property as shown below, only this time, it only returns true or false instead returning class objects containg the events for that day.

日期属性是在路由器序列化器挂钩中使用 controllerFor 设置的,而不是像我们之前将 todayEvent 定义为控制器操作时那样将其传入.

The date property is set using controllerFor in the router serializers hook instead of passing it in as we did when we defined todayEvent as a controller action previously.

 App.CalendarEventController = Em.ObjectController.extend({
    date: null,
    needs: ['appointments'],

    todayEvent: function(){
      var _self = this;
      var appoint = _self.get('controllers.appointments');
      var appCont = appoint.get('content');

      return appCont.map(function(appointee) {
        return (moment(appointee.get('event.start')).unix() == moment(_self.get('date')).unix());    
      });
  }.property('date')    
});

现在我点击约会链接,然后点击日历链接,然后点击日历中的红色日期之一,这样序列化器钩子就可以设置控制器日期,然后我进入控制台:

Now I click on the link for appointment, then the link for calendar and then click one of the dates in red from the calendar, so the serializer hook can set the controller date and I then go into the console:

u = App.__container__.lookup("controller:calendarEvent")

尝试在控制台中获取该日期发生的事件:

try to fetch the events occuring on that date in the console with:

u.get('todayEvent')

我要么得到一个像这样的空数组 [ ] 或者如果我使用 ma​​p() 而不是 filter() 过滤,那么它返回[假,假,假]

I either get an empty array like this [ ] or if I filter using map() instead of filter(), then it returns [false, false, false]

jsfiddle

推荐答案

看来您需要将 'content.@each' 添加到您的计算属性中.

It looks like you need to add 'content.@each' to your computed property.

就目前而言,todayEvent"只会在日期"更改时计算,我猜测日期是在内容之前或同时设置的.

As it stands now 'todayEvent' will only be computed when 'date' changes I am guessing date is being set before or at the same time as the content.

tod​​ayEvent 返回 [false, false],因为您使用的是地图而非过滤器.

todayEvent is returning [false, false] because you are using map not filter.

todayEvent: function(){
  var _self = this;
  var appoint = _self.get('controllers.appointments');
  var appCont = appoint.get('content');

  return appCont.filter(function(appointee) {
    return (moment(appointee.get('event.start')).unix() == moment(_self.get('date')).unix());    
  });
}.property('content.@each', 'date')

这篇关于Emberjs-1.0.0-rc.6 使用 enumerable 列出特定日期发生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆