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

Issue binding a click event handler within a template item

2 Answers 176 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
London TIO
Top achievements
Rank 1
London TIO asked on 18 Mar 2014, 05:47 PM
Hello, I'm trying to bind an array of primitive objects to an un-ordered list using the example from the getting started documentation here:

http://docs.telerik.com/kendo-ui/getting-started/framework/mvvm/bindings/source#source-binding-to-array-of-primitive-objects

I'd like to add a button along side each list item that, when clicked, removes the item from the list.

When I run the example above without the addition of a click event handler, everything works fine as the below link shows:

http://jsbin.com/muzameko/3/edit

However, when I add a click event handler to the same span, or even a neighboring span element as below...

http://jsbin.com/muzameko/4/edit

I get the following error in Chrome's console:

Uncaught TypeError: Object Coffee has no method 'get'

and the rest of the list fails to render correctly. Is this a bug or am I binding in an unusual way?

Thanks in advance, Sam.

2 Answers, 1 is accepted

Sort by
0
London TIO
Top achievements
Rank 1
answered on 19 Mar 2014, 03:35 PM
*Edit*

I've realised that I'm actually binding the click event to a non-existent function i.e. 'viewModel.products.handleClick, where 'products' is the source of the template, in actual fact I need to target 'viewModel.handleClick'.

So I'll change my question, is there anyway to point to an event handler that exists outside of the 'source' binding such as:

<li>
    <span data-bind="text: this, click: viewModel.handleClick"></span>
</li>
0
Alexander Valchev
Telerik team
answered on 20 Mar 2014, 02:36 PM
Hello,

The scenario which you are trying to implement is not supported. The reason is that Kendo MVVM does not wrap the primitive values into Observable object. Primitive objects does not have a parent method (Observable Object has) and as a result the click binding cannot be resolved.

In case you would like to bind the click event, please consider source binding the UI to array with objects.

Regards,
Alexander Valchev
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
London TIO
Top achievements
Rank 1
Answers by
London TIO
Top achievements
Rank 1
Alexander Valchev
Telerik team
Share this question
or