Skip to content

Instantly share code, notes, and snippets.

@ThePendulum
Created December 21, 2016 16:36
Show Gist options
  • Save ThePendulum/3ed3e65e38fde3956b22b67e331ae87a to your computer and use it in GitHub Desktop.
Save ThePendulum/3ed3e65e38fde3956b22b67e331ae87a to your computer and use it in GitHub Desktop.
const items = [{
id: 'foo',
index: 0
}, {
id: 'bar',
index: 1
}, {
id: 'lipsum',
index: 2
}];
// DRAG SORTING OCCURS!
[{
id: 'foo',
index: 0
}, {
id: 'bar',
index: 2
}, {
id: 'lipsum',
index: 1
}];
// How to inform the server?
// Method 1
server.update(items);
// Here are the items with their new index, update them mindlessly
// Advantage: straight forward
// Disadvantage: different clients need to ensure they handle the same sorting rules
// Method 2
server.sort({
id: 'bar',
newIndex: 2
});
// Here is the item that moved and its new index, you do the rest
// Advantage: client-side code is not concerned with how items are sorted
// Disadvantage: complicated, server has to find related items and reindex them
// exactly as the user intended in the UI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment