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

Accessing grid object via MVVM

1 Answer 264 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 04 Nov 2015, 03:27 PM

Hello, 

Is there a better way to get the grid object? Is there MVVM best practice to this approach?

 

HTML:

<div id="model">
    <input type="button" value="ClickMe" data-bind="events:{click: onClick}">
    <div id="grid" data-role="grid" data-bind="source: gridDS" data-columns="[
    { field: 'Creatives_id', title:'ID'},
    { field: 'Email', title: 'User' },
    { field: 'State' }]"></div>
</div>

 

JavaScript

var model = kendo.observable({
    onClick: function(e) {
        var grid = $("#grid").data("kendoGrid");
 
        grid.select().each(function(e) {
            var dataItem = grid.dataItem(this);
            dataItem.State = 1;
            dataItem.dirty = true;
        });
 
        grid.refresh();
    },
    gridDS: new kendo.data.DataSource({...})
});
 
kendo.bind($("#model"), model);

 

Thank you!

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 06 Nov 2015, 09:52 AM
Hello Igor,

In general, the used approach is viable, because there are no connections between the clicked button and the grid. If you would like to avoid retrieving the object using jQuery on click, then you can create an object in the app namespace that can hold the instances of the created widgets. Something like this:
//kendo.bind is called here
 
window.[your app namespace].widgets["grid1"] = $("#grid").data("kendoGrid");

Of course, this is just a spyke and the concrete implementation is up to your personal preferences.

Regards,
Georgi Krustev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Igor
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or