Note that there are some explanatory texts on larger screens.

plurals
  1. POCircular dependency between JavaScript Class and jQuery object
    primarykey
    data
    text
    <p>I'm trying to take an existing working code base and make it object oriented using JavaScript. My system takes JSON containing <em>groups</em> and <em>items</em> in a one-to-many relationship, and visualizes this on a page. The items can be moved into different groups, and their positioning within those groups also needs to be calculated. As such, events will need to be established which are aware of both the groups and tickets around them.</p> <p>I'm using <a href="http://ejohn.org/blog/simple-javascript-inheritance/" rel="nofollow">John Resig's</a> simple JavaScript inheritence setup to establish two classes, <code>Item</code> and <code>Group</code>. When each <code>Item</code> is instantiated it refers back to it's parent <code>Group</code>. <strong>My issue</strong> arises when I want to establish my events, and is most easily explained by the following function:</p> <pre><code>var Group = Class.extend({ ... // Calculate where to place the new item within the group calculate_new_position: function(item) { var pos = -1; // Loop through all DOM items in groups body $(".item", this.elBody).each(function(i) { // Retrieve it's class object var next = $(this).data("_obj"); // Calculating things using the class reference var lowerPrio = item.tData.priority &lt; next.tData.priority, lowerId = item.id &lt; next.id; if(lowerPrio || lowerId) { pos = i; return false; } }); return pos; }, ... )}; </code></pre> <p>Note the use of the <code>.data("_obj")</code> in the above snippet. Essentially, when I need to do something like sorting the items, then I need to know the object (model) corresponding to every item's DOM (view/controller) in the group. Now, I could establish my <code>Group</code> class so that when I create each <code>Item</code> I add a reference to it from within my <code>Group</code> (e.g. so <code>Group.items = [i1, i2...]</code>), and then rather than iterating over the DOM elements, I could iterate over the <code>Item</code> instances. However I think I'd run into similar problems down the line, such as when I want to move an <code>Item</code> into a different <code>Group</code> (as the <code>Group</code> would not know about the <code>Item</code>). </p> <p><strong>Long story short</strong>: is it intrinsically dangerous/naive/pointless to have a class which when instantiated creates a DOM element, which then in turn points back to the class? This feels like circular dependency, and some recursive nightmare, but perhaps a reference to an object isn't such a terrible thing. If I'm doing anything else <em>really</em> stupid, and there's a much simpler way, then please point that out as well.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload