tags:

views:

200

answers:

2

I've got a bit of a problem with some jquery. The code works fine, but it's running too quickly, even inside of the whole $.document(ready) thing.

Essentially, I have a layer of data loaded from a database - and then the .sortable is applied to it.

The .sortable stuff is being applied before the HTML finishes drawing. Any ideas?

The marked area below is where the problem is. /Clouds/List is an ActionResult in ASP.NET MVC that gets the listing and draws the partial view - but it takes longer to do that than the jQuery is executing.

<script type="text/javascript">
    $(function () {
     $("#floating").load("/Tags/List");
     **$("#listing").load("/Clouds/List");**

     $(".sortable").sortable({
      connectWith: '.connectable',
      dropOnEmpty: true,
      receive: function (event, ui) {
       var tag = $(ui.item).attr("id").replace(/t/, "");
       var parent_id = $(ui.item).parent().attr('id');
       $.post("/Clouds/Insert", { cloud: parent_id, tag: tag });
      },
      remove: function (event, ui) {
       var tag = $(ui.item).attr("id").replace(/t/, "");
       var parent_id = $(ui.item).parent().attr('id');
       $.post("/Clouds/Remove", { cloud: parent_id, tag: tag });
      }
     }).disableSelection();

    });
</script>
+8  A: 

You can use the callback argument of $.load, to excute the sortable method after the HTML has been injected to the DOM:

   $("#listing").load("/Clouds/List", function () {
     $(".sortable").sortable({
            connectWith: '.connectable',
            dropOnEmpty: true,
            receive: function (event, ui) {
                    var tag = $(ui.item).attr("id").replace(/t/, "");
                    var parent_id = $(ui.item).parent().attr('id');
                    $.post("/Clouds/Insert", { cloud: parent_id, tag: tag });
            },
            remove: function (event, ui) {
                    var tag = $(ui.item).attr("id").replace(/t/, "");
                    var parent_id = $(ui.item).parent().attr('id');
                    $.post("/Clouds/Remove", { cloud: parent_id, tag: tag });
            }
      }).disableSelection();
   });
CMS
This worked wonderfully!
Stacey
This is awesome. This has expanded my knowledge of jQuery so much, I cannot believe how useful this is. It removes so many of the problems I've encountered with jQuery running before other JavaScript. Absolutely ingenius! Thank you so, so, so much for your help.
Stacey
You're welcome Stacey! I'm glad to help!
CMS
+2  A: 

JQuery's load function accepts a callback that is called when the response is received. Effectively, you can make sure that sortable will not be called before the data is loaded.

<script type="text/javascript">
    $(document).ready( function () {
        $("#floating").load("/Tags/List");
        $("#listing").load("/Clouds/List", {}, function() {

            $(".sortable").sortable({
                    connectWith: '.connectable',
                    dropOnEmpty: true,
                    receive: function (event, ui) {
                            var tag = $(ui.item).attr("id").replace(/t/, "");
                            var parent_id = $(ui.item).parent().attr('id');
                            $.post("/Clouds/Insert", { cloud: parent_id, tag: tag });
                    },
                    remove: function (event, ui) {
                            var tag = $(ui.item).attr("id").replace(/t/, "");
                            var parent_id = $(ui.item).parent().attr('id');
                            $.post("/Clouds/Remove", { cloud: parent_id, tag: tag });
                    }
            }).disableSelection();
        });
    } );
</script>
Louis-Philippe Huberdeau
I do not know who you are, but I love you, too.
Stacey