diff --git a/examples/unfinished/shark/client/shark.js b/examples/unfinished/shark/client/shark.js index 11beefd486..cdfe5f1b27 100644 --- a/examples/unfinished/shark/client/shark.js +++ b/examples/unfinished/shark/client/shark.js @@ -1,8 +1,3 @@ -Items = new Meteor.Collection(null); -Items.insert({ text: 'Foo Greenspan' }); -Items.insert({ text: 'Bar Oliver' }); -Items.insert({ text: 'Beef Tofu' }); - Meteor.startup(function () { /* Meteor.setTimeout(function () { @@ -15,24 +10,37 @@ Meteor.startup(function () { UI.body.name = 'David'; -UI.body.items = Items.find({}, { sort: { text: 1 }}); +UI.body.items = Items.find({}, { sort: { rank: 1 }}); // xcxc rendered didn't work and was surprising. UI.body.attached = function () { - $('#list').sortable(); -}; + $('#list').sortable({ + stop: function (event, ui) { + // xcxc use jQuery .prev() instead of previousElementSibling -// TO REPRO FAILURE: -// 1. Move "Bar Oliver" to the bottom of the list -// 2. Call `funkyReorder()`. -var funkyIteration = 0; -funkyReorder = function () { - var chars = "THEMAGICALMACHINETHATDOESNTBREAKNOMATTERWHATYOUDOWITHIT"; - Items.find({}, {sort: {text: 1}}).forEach(function (item) { - Items.update(item._id, { - text: chars[funkyIteration++] + item.text.slice(1) - }); + el = ui.item[0]; // unbox jQuery array + var before = el.previousElementSibling; + var after = el.nextElementSibling; + + var doc = el.$ui.data(); + + // xcxc improve rank generation. + var newRank; + if (before === null) { // moving to the top of the list + var firstDoc = after.$ui.data(); + newRank = firstDoc.rank-1; + } else if (after === null) { // moving to the bottom of the list + var lastDoc = before.$ui.data(); + newRank = lastDoc.rank+1; + } else { + var beforeDoc = before.$ui.data(); + var afterDoc = after.$ui.data(); + newRank = (beforeDoc.rank + afterDoc.rank) / 2; + } + + Items.update(doc._id, {$set: {rank: newRank}}); + } }); -}; \ No newline at end of file +}; diff --git a/examples/unfinished/shark/lib/items.js b/examples/unfinished/shark/lib/items.js new file mode 100644 index 0000000000..c7482718df --- /dev/null +++ b/examples/unfinished/shark/lib/items.js @@ -0,0 +1,11 @@ +Items = new Meteor.Collection("items"); + +if (Meteor.isServer) { + Meteor.startup(function () { + if (Items.find().count() === 0) { + Items.insert({ text: 'Foo Greenspan', rank: 1 }); + Items.insert({ text: 'Bar Oliver', rank: 2 }); + Items.insert({ text: 'Beef Tofu', rank: 3 }); + } + }); +}