javascript – Emberjs使用sortProperties按日期排序内容
作者:互联网
我试图使用Emberjs sortProperties在这个jsfiddle中按日期排序内容.我的模型有一个startTime属性,我试图排序但它不起作用.然后我在控制器中创建了一个名为todayEvent的计算属性,该属性返回与传入日期匹配的事件,我尝试对其进行排序,但它也无效.我所要做的就是列出在同一天发生的事件,但是以chich事件中发生的一天中的时间按照升序排序,例如,在早上10点之前发生的事件应该在下午12点之前列出.
这是jsfiddle
该模型:
App.TimeSlot = DS.Model.extend( {
startTime: DS.attr('date'),
endTime: DS.attr('date'),
allDay: DS.attr('boolean'),
soldOut: DS.attr('boolean')
});
控制器
App.TimeSlotController = Ember.ArrayController.extend({
content: [ ],
sortProperties: ['todayEvent'],
sortAscending: true,
day: Ember.A(['2013-10-25']),
todayEvent: function(){
self = this;
u = self.get('content');
console.log('u', u);
kl = u.filter(function(availableSlot) {
console.log ('a', availableSlot.get('startTime') );
return (moment(availableSlot.get('startTime')).format("YYYY-MM-DD") == self.get('day').toString() );
});
return kl;
}.property('day', 'content@each'),
});
fixtureAdapter
App.TimeSlot.FIXTURES = [
{
id: 3,
startTime: moment.utc('2013-10-25T12:30:00+01:00',"YYYY-MM-DD HH:mm:ss" ),
allDay: true
},
{
id: 4,
startTime: moment.utc('2013-10-25T10:10:00+01:00',"YYYY-MM-DD HH:mm:ss" ),
allDay: true
},
{
id: 5,
startTime: moment.utc('2013-10-23 00:00 +01:00', "YYYY-MM-DD HH:mm"),
allDay: true
}
];
解决方法:
我设法解决了@edu,但感谢您的帮助.你可以看到工作jsfiddle.
有两种方法,都使用Ember.computed.sort:
sortedTime: Ember.computed.sort('todayEvent', function(a, b){
if (
moment(Ember.get(a, 'startTime') ) > moment(Ember.get(b, 'startTime') )
){
//returns morning timings before afternoon timings
//api doc says return 1 when 'a' should come after 'b'
//http://emberjs.com/api/#method_computed_sort
return 1;
} else if (
moment(Ember.get(a, 'startTime') ) < moment(Ember.get(b, 'startTime') )
)
{
//returns afternoon timings before morning timings
//api docs says return -1, when 'a' should come before 'b'
return -1;
}
return 0;
})
第二种方法
//adapted from http://jsbin.com/OjoXOqE/9/edit
sortedContent: Ember.computed.sort('content.@each.startTime', function(a, b){
//the this keyword does not work here,
//throws "Object #<Object> has no method 'get'"
//so we use the Ember keyword to get it working
var ap = moment(Ember.get(a, 'startTime')),
bp = moment(Ember.get(b, 'startTime'))
//we return morning before afternoon times
if(ap !== bp) {
return ap - bp;
}
})
标签:javascript,ember-js,ember-data 来源: https://codeday.me/bug/20190715/1467743.html