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

Function calls from Templates

11 Answers 227 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 09 Mar 2012, 07:11 PM
I've just tried to convert my original ViewModel to a Kendo-based viewmodel, in hopes I'd be able to use this model to pick up functions from my custom template assigned to a Grid. This, however, doesn't seem to be the case.

Right now I have:

<script id="gridTemplate" type="text/x-kendo-template">
         <span class="k-icon k-arrow-next cursor detailLink" cId="#= ContactID #" data-bind="click: doSomething"></span>
</script>

With the template assigned to my Grid, and doSomething as a function within my Kendo ViewModel. The Grid only gets loaded when a user presses a button on the page, which may have something to do with it - but if it has, then it takes out the point of a View Model always being active.

Is this a known issue in the beta?

11 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 12 Mar 2012, 09:51 AM
Hello,

 Indeed this is a known limitation in the beta. Event bindings in templates are resolved using the template context (the current data item). This means that you need to have a "doSomething" function in all your data items which I agree is inconvenient. We are currently working on resolving this limitation by looking up the View-Model hierarchy to find the right method.

All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Chris
Top achievements
Rank 1
answered on 12 Mar 2012, 10:49 AM
Thanks for confirming this Atanas, and great job with Kendo UI so far.
0
Luc
Top achievements
Rank 1
answered on 17 Apr 2012, 09:14 PM
Hi,

I'd like to know the status on this as I seem to be hitting the same brick wall right now.

Thanks
0
Chris
Top achievements
Rank 1
answered on 18 Apr 2012, 08:43 AM
Hi Luc,

Are you using the latest version of Kendo UI? I got this working when the latest version was released.
0
Petyo
Telerik team
answered on 18 Apr 2012, 09:18 AM
Hi,

I can confirm that this behavior is updated for the official release. 

Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Luc
Top achievements
Rank 1
answered on 18 Apr 2012, 10:37 AM
Yes, you're right. Problem was with my code.

Sorry.
0
Luc
Top achievements
Rank 1
answered on 19 Apr 2012, 11:52 PM
Ok,

Back for more on this issue.

As I'm testing different scenarios, I found that though this change has been implemented in latest build, it seems to only work if the source binding is set to a local array of values. I can't get it to work if the source is a DataSource object. The click handler is not called.

Regards
0
Luc
Top achievements
Rank 1
answered on 20 Apr 2012, 02:52 AM
I'm kind of shooting in the dark here, but the problem seems to be that if I have a DataSource within an ObservaleObject, then its "parent()" function will return undefined.

On the other hand, if I have a javascript array within an ObservableObject, then calling parent() on it will indeed return the ObservableObject.

This seems to be crucial because of this bit of code in kendo:

var EventBinding = Binding.extend( {
   get: function() {
   var source = this.source,
   path = this.path,
   handler;
 
   handler = source.get(path);
 
   while (!handler && source) {
     source = source.parent();
     if (source instanceof ObservableObject) {
       handler = source.get(path);
     }
   }
 
   return proxy(handler, source);
   }
});

on the line:

source = source.parent();

we "walk up" the hierarchy to find an appropriate function for the event binding. In the case of a regular array item, we will eventually find the ObservableObject, but in the case of a DataSource the ObservableObject is never found.
 I hope this helps solve this issue.

Regards
0
Luc
Top achievements
Rank 1
answered on 20 Apr 2012, 05:39 AM
More on this,

I have tracked down the source of the problem (or at least I think I did).

Here's a diff of what I changed:

--- C:/Users/luc/Documents/KendoUI/source/js/kendo.all.js   Fri Apr 06 18:00:56 2012
+++ C:/Users/luc/Documents/Visual Studio 11/Projects/WebApi1/WebApi1/Scripts/kendo.all.js   Fri Apr 20 00:17:17 2012
@@ -4195,6 +4195,8 @@
                         that.trigger(CHANGE, { field: field, index: e.index, items: e.items, action: e.action});
                     });
                 })(field);
+            } else if (object instanceof DataSource) {
+                object.parent = parent;
             }
  
             return object;
@@ -5652,7 +5654,10 @@
         _observe: function(data) {
             var that = this,
                 model = that.reader.model,
-                wrap = false;
+                wrap = false,
+                parent = function () {
+                    return that;
+                };
  
             if (model && data.length) {
                 wrap = !(data[0] instanceof model);
@@ -5665,6 +5670,7 @@
                 }
             } else {
                 data = new ObservableArray(data, that.reader.model);
+                data.parent = parent;
             }
  
             return data.bind(CHANGE, proxy(that._change, that));

Basically, when wrapping the return from the DataSource.read() in _observe(), we need to also assign the parent of the newly created ObservableArray.

Also, in ObservableObject.wrap(), we need to take into account the case where we are wrapping a DataSource object and also assign its parent.

Please review this patch and let me know if it induces unwanted side-effect. I've been working on this for almost 6 hours straight, and I'm not sure I'm still seeing straight enough.

Regards
0
Rosen
Telerik team
answered on 24 Apr 2012, 01:05 PM
Hi Luc,

We have been able to address the issue you have described and the fix will be available with the next internal build. 

Regards,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Luc
Top achievements
Rank 1
answered on 24 Apr 2012, 06:25 PM
Great !

Thanks a lot !
Tags
MVVM
Asked by
Chris
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Chris
Top achievements
Rank 1
Luc
Top achievements
Rank 1
Petyo
Telerik team
Rosen
Telerik team
Share this question
or