0

I'm building a Meteor app that let's the user organize lists of items in tags.

I use jQuery draggable and droppable to update a collection when a user drags an item from one tag to another.

I find it hard to understand how/where/when I should call the function. I've tried a few different options (including this "hacky way of doing it". The Blaze documentation mentions that functions can be called on DOM events, but lacks the drag and drop events that I'm looking for. I've currently settled on calling the function under Template.rendered, but that means the item can only be dropped once per render. I've tried to counter this with Tracker.autorun, but I don't think I understand how it works and the item can still only be dropped once per render.

How can I make the .item draggable several times per render?

Template.tag.rendered = function () {
  //wait on subscriptions to load
  if (Session.get('DATA_LOADED')) {

    Tracker.autorun(function () {
      $(".item").draggable({
        revert: true,

        start: function (event, ui) {
          var movingItem = Blaze.getData(this)._id;
          Session.set('movingItem', movingItem);
        },

      });

      $(".tag").droppable({
        hoverClass: 'droppable',
        drop: function () {
          var movingItem = Session.get('movingItem');
          var acceptingTag = Blaze.getData(this)._id;
          Items.update(movingItem, {
            $set: {"parents": acceptingTag}
          });
        }
      });

    });
  }
};
Community
  • 1
  • 1
vkrogh
  • 31
  • 3

1 Answers1

3

I found the solution.

By separating the .draggable and .droppable into two different Template.rendered the function is now correctly called each time an item is moved.

No need for the Tracker.autorun

Template.item.rendered = function () {
  if (Session.get('DATA_LOADED')) {

      $(".item").draggable({
        revert: true,

        start: function (event, ui) {
          var movingItem = Blaze.getData(this)._id;
          Session.set('movingItem', movingItem);
          console.log('moving ' + movingItem);
        },
      });
  }
};

Template.tag.rendered = function () {
  if (Session.get('DATA_LOADED')) {

      $(".tag").droppable({
        hoverClass: 'droppable',
        drop: function () {
          var movingItem = Session.get('movingItem');
          var acceptingTag = Blaze.getData(this)._id;
          Items.update(movingItem, {
            $set: {"parents": acceptingTag}
          });
          console.log('Dropped on ' + acceptingTag);
        }
      });
  }
};
vkrogh
  • 31
  • 3