Skip to content

Instantly share code, notes, and snippets.

@shouse
Last active September 22, 2017 19:50
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 shouse/06f447884fbc85ec122c to your computer and use it in GitHub Desktop.
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
Copy link

pcflmb commented Oct 30, 2015

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
Copy link
Author

shouse commented Sep 22, 2017

Updated, thanks

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