2015-06-20 48 views
0

我的使用案例如何將位置字段應用於Meteor mongo中的集合?

我有一個在線音樂播放器的播放列表。目前,它們在顯示時按自然順序排序。

我想在播放列表中的軌道位置放置一個字段。

什麼是最好的方式來實現這個集合在流星蒙戈db。

這是我的歌曲集合的當前模式。

//Schema for Songs 
    Schema.Songs = new SimpleSchema({ 
     trackId: { 
     type: String, 
     label: "Track ID", 
     optional: false 
     }, 
     title: { 
     type: String, 
     label: "Name", 
     optional: false 
     }, 
     duration:{ 
     type: Number, 
     label: "Duration", 
     optional: false 
     }, 
     festivalId: { 
     type: SimpleSchema.RegEx.Id, 
     optional: false 
     } 
    }); 

我希望能夠重新排序的歌曲,例如一首歌曲在位置3.我想移動到位置1,然後將所有的其他歌曲位置字段會適當地更新。

這會是一個好的起點嗎?

回答

0

想到最簡單的方法是獲得一個名爲playlist的集合,其名稱爲songssongs字段將是一個字符串數組。這將是Songs

Schema.Playlist = new SimpleSchema({ 
    'songs.$': { 
    type: String, 
    } 
}) 

ID的然後,您可以創建一個幫助列出所有song。我們稱之爲showPlaylist。它將songs字段中的ID解析爲其透視文檔。我會讓由showPlaylist反應的數組返回。這會爲你節省一些工作。

{{ #each showPlaylist }} 
    <audio id="{{ _id }}" class="song" src="{{ src }}"></audio> 
{{ /each }} 

現在你必須寫一些東西,爲用戶重新排序列表,並稱之爲:

function updatePlaylistOrder (oldPos, newPos, id) { 
    var newList = moveElement(Playlists.findOne(id), oldPos, newPos) 
    Playlists.update(playlistId, {$set: {songs: newList}}) 
    return newList 
} 

Credit for the moveElement function/u/Matt

相關問題