我有一个错误,计算属性会暂时变为不正确的值。
JSBin复制错误:http://jsbin.com/yufet/6/edit?js,console
// ember.js 1.6.0-beta.1+canary.8d91bf06
// ember-data 1.0.0-beta.7+canary.02d7c7be
App.Playlist = DS.Model.extend({
tracks: DS.hasMany('track', { inverse: 'playlist' }),
sortProperties: ['trackOrder:asc'],
sortedTracks: Ember.computed.sort('tracks', 'sortProperties'),
queuedTracks: Ember.computed.filterBy('sortedTracks', 'isQueued'),
currentTrack: Ember.computed.alias('queuedTracks.firstObject')
});
App.Track = DS.Model.extend({
playlist: DS.belongsTo('playlist', { inverse: 'tracks' }),
title: DS.attr('string'),
trackOrder: DS.attr('number'),
isOrdered: Ember.computed.notEmpty('trackOrder'),
playedAt: DS.attr('date'),
hasPlayed: Ember.computed.notEmpty('playedAt'),
hasNotPlayed: Ember.computed.not('hasPlayed'),
isQueued: Ember.computed.and('isOrdered', 'hasNotPlayed')
});
然后是我的主要人物:
store = App.__container__.lookup('store:main');
store.pushPayload('playlist', {
"playlist": {
"id":1,
"title":"Playlist 1",
"track_ids":[1,2,3,4,5]
},"tracks":[
{ "id":1, "playlist_id":1, "title": "Track 0", "track_order": 5, "played_at":null },
{ "id":2, "playlist_id":1, "title": "Track 1", "track_order": 10, "played_at":null },
{ "id":3, "playlist_id":1, "title": "Track 2", "track_order": 15, "played_at":null },
{ "id":4, "playlist_id":1, "title": "Track 3", "track_order": 20, "played_at":null },
{ "id":5, "playlist_id":1, "title": "Track 4", "track_order": 25, "played_at":null }
]
});
store.find('playlist', 1).then(function(pl) {
playlist = pl;
var observer = Ember.Object.create({
logMessage: function(room) {
console.log('currentTrack changed to', playlist.get('currentTrack.title'));
}
});
playlist.addObserver('currentTrack', observer, 'logMessage');
playlist.get('currentTrack.title');
track = playlist.get('tracks').createRecord({ title: 'A new song!!'});
track.set('trackOrder', 30);
});
输出:
currentTrack changed to A new song!!
currentTrack changed to Track 0
track.trackOrder
仅限null
或30
。当它为null
时,我希望isQueued
为false
并过滤掉该曲目。当它是30
时,它应该在列表的最后。
如果我没有在观察者本身中拨打get('currentTrack.title')
,currentTrack
仍为A new song!!
。
答案 0 :(得分:0)
在Stefan Penner的黑客时刻获得一些帮助后想出来。
您无法在模型上使用sortProperties
,因为它是Ember.SortableMixin
提供的,只会与控制器混合使用。
我认为它实际上并没有排序。哈!