javascript - how to apply a position field to a collection within Meteor mongo? -


my use case

i have playlist online music player. sorted natural order when displayed.

i put in field track position within playlist.

what best way implement collection in meteor mongo db.

here current schema songs collection.

    //schema 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       }     }); 

i able reorder songs, example song @ position 3. move position 1 , other songs position field update appropriately .

what starting point this?

the easiest way comes mind have collection called playlist field called songs. songs field array of strings. these id's of songs

schema.playlist = new simpleschema({   'songs.$': {     type: string,   } }) 

you can create helper list song. let's call showplaylist. resolves id's inside songs field perspective documents. i'd make array returned showplaylist reactive. save work.

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

now you'll have write user reorder list , call this:

function updateplaylistorder (oldpos, newpos, id) {   var newlist = moveelement(playlists.findone(id), oldpos, newpos)   playlists.update(playlistid, {$set: {songs: newlist}})   return newlist } 

credit moveelement function /u/matt


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -