views:

14

answers:

1

I have 2 sortable, connected lists with pics: Album and Favorites.
When I drag and item from Album -> Favorites I want to check if it's already in the Favorites list.

  • If it is, do NOT accept it (maybe disable sorting or something?)
  • If it is not, clone the item back to the original index in Albums (connected sortable lists do only move items, so this mimics copying)
  • I have a function that checks if the pics are in the Favorites list:

    function isInFavorites(url) {
        return $(".favorites li img[src*='" + url + "']").length > 0;
    }
    

    This function works like expected...
    However when I extract the scr attr with ui.item and pass the argument to this function I always get a true boolean??

    var itemSrc = ui.item.find("img").attr("src");
    if (isInFavorites(itemSrc)) { alert('item allready in favorites, do not accept'); } 
    else { alert('OK, now clone back to album'); }
    

    I have been banging my head way to long on this and would appreciate some help!
    A JS Fiddle can be found here: http://jsfiddle.net/tunafish/CTps3/

    Cheers!

    +1  A: 

    Not sure if this is the best way to process the logic but the order the events are firing is the source of your problem

    function isInFavorites(url) {
        return $(".favorites li img[src*='" + url + "']").length > 0;
    }
    

    This event runs AFTER the item has been moved. if it is a duplicate you will have length 2, but you will always have length 1 because you just moved the item into the lower list.

    quick fix is to test for $(".favorites li img[src*='" + url + "']").length > 1

    Bobby Borszich
    That's it! You are right, after the 'receive' method, the item IS in the list. If I use the function with the 'start' method it works like expected. Thanks sooooo much Bobby!!!
    FFish