Brought back the viewModule.getAncestor function.

Extended it, so you can pass actual type instances instead of strings.
This commit is contained in:
Hristo Deshev
2015-08-05 17:02:42 +03:00
parent 141668bb05
commit c5608d7c5c
3 changed files with 62 additions and 33 deletions

View File

@@ -55,6 +55,21 @@ export function eachDescendant(view: definition.View, callback: (child: View) =>
view._eachChildView(localCallback);
}
export function getAncestor(view: View, criterion: string | Function): definition.View {
let matcher: (view: definition.View) => boolean = null;
if (typeof criterion === "string")
matcher = (view: definition.View) => view.typeName === criterion;
else
matcher = (view: definition.View) => view instanceof criterion;
for (let parent: definition.View = view.parent; parent != null; parent = parent.parent) {
if (matcher(parent))
return parent;
}
return null;
}
var viewIdCounter = 0;
function onCssClassPropertyChanged(data: dependencyObservable.PropertyChangeData) {

8
ui/core/view.d.ts vendored
View File

@@ -22,6 +22,14 @@ declare module "ui/core/view" {
*/
export function eachDescendant(view: View, callback: (child: View) => boolean);
/**
* Gets an ancestor from a given type.
* @param view - Starting view (child view).
* @param criterion - The type of ancestor view we are looking for. Could be a string containing a class name or an actual type.
* Returns an instance of a view (if found), otherwise undefined.
*/
export function getAncestor(view: View, criterion: string | Function): View;
/**
* Defines interface for an optional parameter used to create a view.
*/