question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Infinite loop in widget.js when used in an inline with an empty/zero-length queryset.

See original GitHub issue

This particular loop goes on forever: https://github.com/gregmuellegger/django-sortedm2m/blob/master/sortedm2m/static/sortedm2m/widget.js#L22

while (!label || !label.length) {
  currentElement = currentElement.parent();
  label = currentElement.siblings('label');
}

It only happens if the item that needs sorting is empty and uses a Django admin inline. It works fine if it’s not a Django admin inline - when viewing it from that model’s page on the Django admin.

I’ve added logging inside the loop:

while (!label || !label.length) {
  console.log("here");
  console.log(label);
  console.log(currentElement);
  currentElement = currentElement.parent();
  label = currentElement.siblings('label');
}

This is the output:

here
undefined
[ul.sortedm2m-items.hide.sortedm2m, context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div.sortedm2m-container, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div.related-widget-wrapper, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[td.field-deals, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[tr#deal_categories-0.form-row.row1.has_original.dynamic-deal_categories, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[tbody, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[table, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[fieldset.module, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div.tabular.inline-related.last-related, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div#deal_categories-group.inline-group, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[form#hotel_form, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div#content-main, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div#content.colM, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[div#container, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[body.flat-theme.app-hotels.model-hotel.change-form, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[html, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
[document, prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[1], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]

It then goes on to print this forever:

here
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]
[prevObject: jQuery.fn.init[0], context: ul.sortedm2m-items.hide.sortedm2m]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ishmelev23commented, Oct 1, 2017

Just check ul element on empty in iterateUl . Works fine for me.

function iterateUl() {
            $('.sortedm2m-items').each(function () {
                var ul = $(this);
                if($.trim(ul.html()))
                    prepareUl(ul);
                ul.removeClass('hide');
            });
}
0reactions
clintonbcommented, Dec 13, 2020

Fixed by #170

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite loop in widget.js when used in an inline with an empty ...
It only happens if the item that needs sorting is empty and uses a Django admin inline. It works fine if it's not...
Read more >
Django Infinite scroll using Django Paginate repeating the ...
In the templates I replaced post_list with page_obj see below code. That did it {% if post_list.has_next %} <a class="infinite-more-link" ...
Read more >
Infinite Loops in Javascript - Flexiple
Such a loop is called an infinite loop. In this article, we are going to see how to detect and escape infinite loops....
Read more >
Infinite loop when calling hidden button via OnBlur | OutSystems
In a v10 web app I have a hidden button that is called using the following Javascript when an OnBlur is triggered via...
Read more >
JavaScript while Loop - W3Schools
The while loop loops through a block of code as long as a specified condition is true. Syntax. while (condition) { // code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found