0

What is the proper way to edit items in a listview when using Kendo UI Mobile & MVVM?

I don't get the expected results when using the following:

HTML

<div id="itemsView"
    data-role="view"
    data-model="vm">

    <ul data-role="listview" data-bind="source: items" 
                             data-template="itemsTemplate">
    </ul>
    <script id="itemsTemplate" type="text/x-kendo-template">
    <li>
        #=Name#
    </li>
    </script>

    <input type="text" data-bind="value: newValue" />
    <button data-role="button" data-bind="click: update">update</button>
</div>​

JavaScript

var vm = kendo.observable({
    items: [{
        Name: "Item1"}],
    newValue: '',
    update: function(e) {
        var item = this.get("items")[0];
        item.set("Name", this.get("newValue"));
        //adding the follwoing line makes it work as expected
        kendo.bind($('#itemsView'), vm);
    }
});

kendoApp = new kendo.mobile.Application(document.body, {
transition: "slide"});​

I expect the listview to reflect the change to the Name property of that item. Instead, a new item is added to the listview. Examining the array reveals that there is no additional item, and that the change was made. (re)Binding the view to the view-model updates the list to reflect the change. Re-Binding after a change like this doesn't seem to make any sense.

Here is the jsfiddle: http://jsfiddle.net/5aCYp/2/

4

2 回答 2

0

Not sure if I understand your question properly: but this is how I did something similar with Kendo Web UI, I expect mobile is not so different from Web UI from API perspective.

   $element.kendoListView({
        dataSource: list,
        template: idt,
        editTemplate: iet,
        autoBind: true
    });

The way I bind the listview is different, but I guess you can get similar results with your method as well.

I pass two templates to the list view, one for displaying and one for editing. Display template contains a button (or any element) with css class k-edit to which kendo will automatically bind the listview edit action.

display template:

     <div class="item">
        # if (city) { #
            #: city #<br />
        # } #
        # if (postCode) { #
            #: postCode #<br />
        # } #
         <div class="btn">
            <a href="\\#"><span class="k-icon k-edit"></span>Edit</a>
            <a href="\\#"><span class="k-icon k-delete"></span>Delete</a>
         </div>
    </div>

Edit template

     <div class="item editable">
        <div>City</div>
        <div>
            <input type="text" data-bind="value: city" name="city" required="required" validationmessage="*" />
             <span data-for="city" class="k-invalid-msg"></span>
         </div>
        <div>Post Code</div>
        <div>
             <input type="text" data-bind="value: postCode" name="postCode" required="required" validationmessage="*" />
             <span data-for="postCode" class="k-invalid-msg"></span>
         </div>
         <div class="btn">
             <a href="\\#"><span class="k-icon k-update"></span>Save</a>
             <a href="\\#"><span class="k-icon k-cancel"></span>Cancel</a>
         </div>
    </div>

Clicking that element will put the current element on edit mode using the editTemplate.

Then on the editTemplate there is another button with k-update class, again to which kendo will automatically bind and call the save method on the data source.

Hopefully this will give you more ideas on how to solve your issue.

于 2012-12-06T08:05:47.300 回答
0

The problem was caused by the <li> in the template. The widget already supplies the <li> so the additional <li> messes up the rendering. This question was answered by Petyo in the kendo ui forums

于 2012-12-07T15:55:15.753 回答