Famous/framework

nested $repeats and $index values

Joera opened this issue · 2 comments

Joera commented

Question: Who has an idea how i can get a parent $index value inside a child $repeat?

as in a html structure like

<node class="cluster"> //  first repeat - thinks rows
        <node class="particle"></node> // second repeat  -- think columns 
    </node>

and .js

behaviors: {

.cluster': {

        '$repeat': function(s_array) {
               var result = [];
               for (var i = 0; i < s_array.length; i++) { result.push({}); } 
               return result;
        },
        'position': function($index) 
                return [ s_array[$index][1], ];  
        },

},
'.particle': {

        '$repeat': function(s_array) {
            var result = [];
            for (var i = 0; i < s_array[PARENTINDEX][2]; i++) { result.push({}); } 
            return result;
        },
        'position': function() {
                return [$index, 0];      
        },

},

states: {

  s_array : [[1,11,8],[2,9,13],[2,9,23],[3,7,13],[4,6,19],[5,5,19],[6,4,8],[6,19,5],[7,4,7],[7,21,2],[8,4,6]]  

}

You can create cluster as its own component, then do a repeat on the parent, passing the index down to cluster. This index in cluster will be the column number. Then cluster can have its own repeat inside of itself, which will be the row number.

Joera commented

Ah .. nesting components ..Thanks. I will give it a try.