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

Unable to access correct data context from within detailInit callback on Grid when using Data Attributes and MVVM.

2 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lucas
Top achievements
Rank 1
Lucas asked on 31 Jul 2013, 02:18 AM
Heya Telerik,

I've noticed an inconsistency in how row data is passed to the detailInit callback on the grid widget when I'm using the detailTemplate.  When the Grid widget is loaded via jquery ($("#grid").kendoGrid()) then the detailInit callback receives a event.data argument that is set to the data for that row, as you would expect.  When the Grid widget is loaded via data attributes (data-role="grid") the detailInit callback receives a event.data argument that is set to the dataSource of the entire grid.  This means I am unable to access the Id, or any other data for that matter, for the row i'm interested in.

To demonstrate what I'm talking about, here's a JS fiddle.  Click on the green block to expand the row: http://jsfiddle.net/ducka/SXcT4/

I've traced the behavior back to the "widget" binder in kendo.binder.js:
binders.widget = {
        events : Binder.extend({
            init: function(widget, bindings, options) {
                Binder.fn.init.call(this, widget.element[0], bindings, options);
                this.widget = widget;
                this.handlers = {};
            },
 
            refresh: function(key) {
                var binding = this.bindings.events[key],
                    handler = this.handlers[key];
 
                if (handler) {
                    this.widget.unbind(key, handler);
                }
 
                handler = binding.get();
 
                this.handlers[key] = function(e) {
                    // >> ISSUE: This line here is overwriting the row data with an instance to the binding source of the grid.
                    e.data = binding.source;
 
                    handler(e);
 
                    if (e.data === binding.source) {
                        delete e.data;
                    }
                };
 
                this.widget.bind(key, this.handlers[key]);
            },
 
            destroy: function() {
                var handler;
 
                for (handler in this.handlers) {
                    this.widget.unbind(handler, this.handlers[handler]);
                }
            }
        }),
I assume this is not correct behavior?

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimiter Madjarov
Telerik team
answered on 02 Aug 2013, 07:58 AM
Hi Lucas,


Thank you for providing the example to demonstrate what are you referring to. In the current implementation, this is the expected behavior by design i.e. when the Grid is bound via MVVM, the context is set to the observable object itself.

If you have any further questions about Kendo UI do not hesitate to contact us. I wish you a great day!

 

Regards,
Dimiter Madjarov
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Lucas
Top achievements
Rank 1
answered on 04 Aug 2013, 03:06 AM
Oh ok, thats a shame,

Just so you know, i think this behavior needs to be consistent across both methods of initialising the kendo grid.  When defining a detail template, I want to be able to access the data context of the row by default more than the data context of the entire grid.   Having said that, it is useful to also have easy access to the parent data context of the row (in the case, the grid's data context).  This is done in knockout js by accessing the $parent and $parents ambient variables from the detail template.

Thanks for the great framework guys!

Tags
Grid
Asked by
Lucas
Top achievements
Rank 1
Answers by
Dimiter Madjarov
Telerik team
Lucas
Top achievements
Rank 1
Share this question
or