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

Access Parent Methods from Child Template When Using Array Of Primitive Values As Source

1 Answer 435 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Santiago
Top achievements
Rank 1
Santiago asked on 30 Aug 2016, 09:38 PM

Hi there. I have a problem here... Suppose the following scenario:

I have this model:

 

var vm = kendo.observable({

   values: ["orange", "apple", "etc"],

   remove: function(e) { console.log("this is working"); }

});

and then something like this in my markup:

<div data-template="diosTeAyude" data-bind="source: values"></div>
<script id="diosTeAyude" type="text/x-kendo-template">
        <div class="input-group">
            <input data-bind="value: this">
            <span class="input-group-addon" data-bind="click: remove">-</span>
        </div>
</script>

 

"remove" method is not accessible, cause I'm using primitive types in my array, nothing outside the actual primitive value is there. If I change my model to:

var vm = kendo.observable({
values: [{theValue: "orange"}, {theValue: "apple"}, {theValue: "etc"}],
remove: function(e) { console.log("this is working"); }
});

and my template to:

<script id="diosTeAyude" type="text/x-kendo-template">
        <div class="input-group">
            <input data-bind="value: theValue">
            <span class="input-group-addon" data-bind="click: remove">-</span>
        </div>
</script>

then the viewModel prototypes fine and remove function is there, even when convert my array of strings to objects is unnecessary from the model point of view.

 

Is there any other way to accomplish what I need without having to do that?

 

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 01 Sep 2016, 11:37 AM
Hello Santiago,

The first example is not working correctly because the template is receiving just a string array, not a model. When there is a function like in this case remove, the model will know for its parent, and can call the function. If the first example does not have a function it will populate the inputs correctly.

Please check our demo which is demonstrating the recommended way of using MVVM source binding with templates:

http://demos.telerik.com/kendo-ui/mvvm/source

Also, please check the information in our documentation for source binding:

http://docs.telerik.com/kendo-ui/framework/mvvm/bindings/source#source-binding

Let me know if you need additional information on this matter.

Regards,
Stefan
Telerik by Progress
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
MVVM
Asked by
Santiago
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or