Skip to content

Instantly share code, notes, and snippets.

@carambula
Last active December 22, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carambula/6445764 to your computer and use it in GitHub Desktop.
Save carambula/6445764 to your computer and use it in GitHub Desktop.
Quick sketch for looping through child views. Not the cleanest but works. Fork if you got somethin better!
// Set up the views. One big view and several smaller views. I'm using a shortcut to make all the subViews the same as the first
parentViewA = new View({x:10,y:10,width:500,height:500})
subView1 = new View({x:0,y:0,width:50,height:50})
subView1.superView = parentViewA
subView2 = new View(subView1)
subView3 = new View(subView1)
subView4 = new View(subView1)
subView5 = new View(subView1)
subView6 = new View(subView1)
// LONGHAND
// Loop through the array of child views of parentViewA one at a time,
// moving them my amount j,
// then increas j by 60
var j=0
for (index in parentViewA.subViews){
//alert(child.name)
parentViewA.subViews[index].x += j
j+=60
}
// SHORTHAND
// Use forEach to loop through the subViews
// initiating an anonymous function with arguments for the childView and the index (count)
// Same as above but elegent as heck. Good one Thomas Aylott!
parentViewA.subViews.forEach(function(childView, index){
childView.y += index * 60
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment