diff --git a/src/Visto/Scripts/libs/visto.js b/src/Visto/Scripts/libs/visto.js index f66f633..26dc62b 100644 --- a/src/Visto/Scripts/libs/visto.js +++ b/src/Visto/Scripts/libs/visto.js @@ -320,6 +320,16 @@ define(["require", "exports", "libs/hashchange"], function (require, exports, __ } exports.getViewFromElement = getViewFromElement; ; + /** + * Gets the parent view model of the given element. + */ + function getViewModelFromElement(element) { + var view = getViewFromElement(element); + if (view !== null && view !== undefined) + return view.viewModel; + return null; + } + exports.getViewModelFromElement = getViewModelFromElement; /** * 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. @@ -1035,6 +1045,18 @@ define(["require", "exports", "libs/hashchange"], function (require, exports, __ Parameters.prototype.getObject = function (key, defaultValue) { return this.getObservableObject(key, defaultValue)(); }; + /** + * Gets a function parameter and sets the this/caller to the parent view model if no caller is set. + */ + Parameters.prototype.getFunction = function (key, viewModel) { + var func = this.getObject("click"); + if (func.caller !== undefined && func.caller !== null) + return func; + return function () { + var parentViewModel = viewModel.view.viewParent.viewModel; + return func.apply(parentViewModel, arguments); + }; + }; Parameters.prototype.setValue = function (key, value) { var observable = this.getObservableObject(key, value); observable(value); diff --git a/src/Visto/Scripts/libs/visto.ts b/src/Visto/Scripts/libs/visto.ts index 577014d..796485a 100644 --- a/src/Visto/Scripts/libs/visto.ts +++ b/src/Visto/Scripts/libs/visto.ts @@ -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 = >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. @@ -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("click"); + if (func.caller !== undefined && func.caller !== null) + return func; + + return function () { + var parentViewModel = (viewModel).view.viewParent.viewModel; + return func.apply(parentViewModel, arguments); + }; + } + setValue(key: string, value: T) { var observable = this.getObservableObject(key, value); observable(value);