Skip to content

Instantly share code, notes, and snippets.

@shouse
Last active September 22, 2017 19:50
Show Gist options
  • Select an option

  • Save shouse/06f447884fbc85ec122c to your computer and use it in GitHub Desktop.

Select an option

Save shouse/06f447884fbc85ec122c to your computer and use it in GitHub Desktop.
Iterate through titanium views and look for class
/**
* @function getViewByClass
* @summary This will take a class and an optional parent and find children with that class
* @param {string} _class Class name
* @param {object} _parent Optional Parent View to iterate through
* @param {number} _depth Optional depth of recursiveness
* @returns {array} Array of views with the class
*
* @TODO Implement _depth and recursive calls
*/
exports.getViewByClass = function(_class, _parent, _depth) {
_parent = _parent || $.main;
var classArray = [];
_.each(_parent.children, function(child){
if (child.className === _class) {
classArray.push(child);
}
});
return classArray;
};
/**
* @function getViewByClass
* @summary This will take a class and an optional parent and find children with that class
* @param {string} _element Element name
* @param {object} _parent Optional Parent View to iterate through
* @param {number} _depth Optional depth of recursiveness
* @returns {array} Array of views with the class
*
* @TODO Implement _depth and recursive calls
*/
exports.getViewByElement = function(_element, _parent, _depth) {
/* DOES NOT WORK AT ALL - NEED TO CHECK OUT THE CHILD PROPERTIES */
_parent = _parent || $.main;
var classArray = [];
_.each(_parent.children, function(child){
if (child.className === _class) {
classArray.push(child);
}
});
return classArray;
};
@pcflmb

pcflmb commented Oct 30, 2015

Copy link
Copy Markdown

You need to replace class.push(child); with classArray.push(child); to get it working.

Also, the 3rd @param should be named _depth, not _parent.

@shouse

shouse commented Sep 22, 2017

Copy link
Copy Markdown
Author

Updated, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment