mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
first attempt at a working sortable list
This commit is contained in:
@@ -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}});
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
11
examples/unfinished/shark/lib/items.js
Normal file
11
examples/unfinished/shark/lib/items.js
Normal file
@@ -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 });
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user