Skip to content

Commit

Permalink
Add: Meteor custom collection item insert feature (Fix: SortableJS#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascoual committed Aug 26, 2015
1 parent c760568 commit fd3e8e2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
18 changes: 18 additions & 0 deletions meteor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ Template.myTemplate.helpers({
```


## More control ?

If you want to manage the collection item insert yourself, just use onAdd
option and set event.stopDefaultMeteorPropagation to true.
Do all you needs in onAdd function.

You can create your server Meteor method and call it from onAdd.

```js
onAdd: function (event) {
...
event.stopDefaultMeteorPropagation = true;
Meteor.call('your_function', ...);
...
},
```


# Issues

If you encounter an issue while using this package, please CC @dandv when you file it in this repo.
Expand Down
6 changes: 5 additions & 1 deletion meteor/example/client/define-object-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ Template.typeDefinition.helpers({
delete event.data._id; // Generate a new id when inserting in the Attributes collection. Otherwise, if we add the same type twice, we'll get an error that the ids are not unique.
delete event.data.icon;
event.data.type = event.data.name;
event.data.name = 'Rename me (double click)'
event.data.name = 'Rename me (double click)';
// If you want to manage the collection item insert yourself, just use onAdd
// option and set event.stopDefaultMeteorPropagation to true:
// event.stopDefaultMeteorPropagation = true;
// and do all you needs here, in onAdd function.
},
// event handler for reordering attributes
onSort: function (event) {
Expand Down
26 changes: 14 additions & 12 deletions meteor/reactivize.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,20 @@ Template.sortable.rendered = function () {
// let the user decorate the object with additional properties before insertion
if (optionsOnAdd) optionsOnAdd(event);

// Insert the new element at the end of the list and move it where it was dropped.
// We could insert it at the beginning, but that would lead to negative orders.
var sortSpecifier = {}; sortSpecifier[orderField] = -1;
event.data.order = templateInstance.collection.findOne({}, { sort: sortSpecifier, limit: 1 }).order + 1;
// TODO: this can obviously be optimized by setting the order directly as the arithmetic average, with the caveats described above
var newElementId = templateInstance.collection.insert(event.data);
event.data._id = newElementId;
if (itemEl.nextElementSibling) {
var orderNextItem = Blaze.getData(itemEl.nextElementSibling)[orderField];
templateInstance.adjustOrders(newElementId, null, orderNextItem);
} else {
// do nothing - inserted after the last element
if (!event.stopDefaultMeteorPropagation) {
// Insert the new element at the end of the list and move it where it was dropped.
// We could insert it at the beginning, but that would lead to negative orders.
var sortSpecifier = {}; sortSpecifier[orderField] = -1;
event.data.order = templateInstance.collection.findOne({}, { sort: sortSpecifier, limit: 1 }).order + 1;
// TODO: this can obviously be optimized by setting the order directly as the arithmetic average, with the caveats described above
var newElementId = templateInstance.collection.insert(event.data);
event.data._id = newElementId;
if (itemEl.nextElementSibling) {
var orderNextItem = Blaze.getData(itemEl.nextElementSibling)[orderField];
templateInstance.adjustOrders(newElementId, null, orderNextItem);
} else {
// do nothing - inserted after the last element
}
}
// remove the dropped HTMLElement from the list because we have inserted it in the collection, which will update the template
itemEl.parentElement.removeChild(itemEl);
Expand Down

0 comments on commit fd3e8e2

Please sign in to comment.