Skip to content

Commit

Permalink
Minor adds
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Dec 3, 2015
1 parent e89c10c commit 184b98d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Visto/Scripts/libs/visto.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/Visto/Scripts/libs/visto.ts
Expand Up @@ -361,6 +361,16 @@ export function getViewFromElement(element: JQuery): ViewBase {
return null;
};

/**
* Gets the parent view model of the given element.
*/
export function getViewModelFromElement(element: JQuery): ViewModel {
var view = <View<any>>getViewFromElement(element);
if (view !== null && view !== undefined)
return view.viewModel;
return null;
}

/**
* Registers an initializer which is called after the elements of the context are added to the DOM.
* This is used for custom ko bindings so that they work correctly if they assume that an element is already in the DOM.
Expand Down Expand Up @@ -1227,6 +1237,20 @@ export class Parameters {
return this.getObservableObject(key, defaultValue)();
}

/**
* Gets a function parameter and sets the this/caller to the parent view model if no caller is set.
*/
getFunction(key: string, viewModel: ViewModel): any {
var func = this.getObject<any>("click");
if (func.caller !== undefined && func.caller !== null)
return func;

return function () {
var parentViewModel = (<any>viewModel).view.viewParent.viewModel;
return func.apply(parentViewModel, arguments);
};
}

setValue<T>(key: string, value: T) {
var observable = this.getObservableObject<T>(key, value);
observable(value);
Expand Down

0 comments on commit 184b98d

Please sign in to comment.