This is a migrated thread and some comments may be shown as answers.

Accessing custom widget from inner view model

1 Answer 99 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Brian Vallelunga
Top achievements
Rank 1
Brian Vallelunga asked on 18 May 2012, 03:31 PM
I have a custom composite widget that internally uses the MVVM framework for managing its data and behavior. I have the need to have the view model trigger events at the widget level so pages using the widget can monitor its changes.

What I'm having difficulty with is getting a reference to the widget from within its view model, so that I can call the trigger function. Is there a pattern for this?

My code looks roughly like:

MyWidget = Widget.extend {
 
    viewModel: {}
 
    init: function(element, options) {
        this.viewModel = initializeViewModel(this.options)
    }
 
    initializeViewModel: function(options) {
        return new kendo.data.ObservableObject({
            someFunctionThatNeedsAccessToTheWidget: function() {
                // trigger event here
            }
        });
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Iliana Dyankova
Telerik team
answered on 22 May 2012, 03:51 PM
Hi Brian,

In order to get a reference to the widget from within its view model, you could use a javaScript closure. For example: 
initializeViewModel: function(options) {
        var widget = this;
        return new kendo.data.ObservableObject({
            someFunctionThatNeedsAccessToTheWidget: function() {
                // trigger event here
                widget.someMethod();
            }
        });
    }

I hope this helps. 

Kind regards,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
MVVM
Asked by
Brian Vallelunga
Top achievements
Rank 1
Answers by
Iliana Dyankova
Telerik team
Share this question
or