Skip to content

findParentRatio infinite recursion #3

@jbenet

Description

@jbenet

The findParentRatio function:

function findParentRatio(jqObject) {
  var p = jqObject.parent(),
    displayType = p.css('display');

  if (displayType == 'block' || displayType == '-webkit-box' && p.width() > 0) {
    return { obj: p, width: p.width(), height: p.height(), ratio: (p.width() / p.height()) };
  } else {
    return findParentRatio(p);
  }
}

can cause infinite recursion (e.g. when the element is not in the DOM yet).

I suggest something like:

function findParentRatio(jqObject) {
  var p = jqObject.parent(),
    displayType = p.css('display');

  if (!p) {
    return a default or throw error here.
  }

  if (displayType == 'block' || displayType == '-webkit-box' && p.width() > 0) {
    return { obj: p, width: p.width(), height: p.height(), ratio: (p.width() / p.height()) };
  } else {
    return findParentRatio(p);
  }
}

Also, this approach doesn't mesh well with some MVC frameworks (like Backbone),
where views are rendered before they're added to the DOM.

Can't think of another way to do it at the moment though :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions