Refreshing Kendo UI viewModel

4k views Asked by At

I have a very simply page at the moment. It has a first name input, last name input, and a list of names added. You can add your first and last name to the text box, press add. It adds it the peopleList I have and adds a new listItem with their name.

My issue is when I add to the peopleList in my code, it does not update the listView. I think I need to use observable, but I am not exactly sure how to do it. My list shows it has 25 items added to it after I click btnMany, which is how many it show have.

here is the body of my code:

<!--Load Some Data-->
<div id="peopleDefaultView"
    data-role="view"
    data-model="ViewModel"
    data-layout="default">
    <!--View-->
    <input type="text" data-bind="value: firstName" id="fName" />
    <input type="text" data-bind="value: lastName" id="lName" />
    <a data-bind="click: addName" data-role="button" id="btnOne">Add</a>
    <a data-bind="click: setValues" data-role="button" id="btnMany">Add Many</a>
    <div style="margin-top: 10px">
        People List:
        <ul data-template="people-l-template" data-bind="source: peopleList" id="pList"></ul>
    </div>
</div>

<!-- Kendo Template-->
<script id="people-l-template" type="text/x-kendo-template">
    <li>
        FirstName: <span data-bind="text: first"></span>
        LastName: <span data-bind="text: last"></span>
        <a data-bind="click: removeName" data-role="button" id="btnRemoveName">X</a>
    </li>
</script>

And here is my script to go along with it

<script>
    var ViewModel = {
        firstName: '',
        lastName: '',
        peopleList: [],
        addName:  function (e) {
            this.get("peopleList").push({
                first: this.get("firstName"),
                last: this.get("lastName"),
            });
            this.set("firstName", '');
            this.set("lastName", '');
        },
        removeName: function (e) {
            this.set("peopleList", jQuery.grep(this.peopleList, function (item, i) {
                return (item.firstName != e.data.firstName && item.lastName != e.data.lastName);
            }));
        },
        setValues: function (e) {
            GetValueFromServer();
        }
    };

    var GetValueFromServer = function () {
        $.ajax({
            type: "GET",
            url: "GetPeopleService.svc/GetPersonById/",
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                response.forEach(function (person) {
                    ViewModel["peopleList"].push({
                        first: person.firstName,
                        last: person.lastName
                    });
                });
                alert(ViewModel.peopleList.length);
            },
            error: function (response) {
                console.log(response);
            }
        });
    };

    var application = new kendo.mobile.Application(document.body);
</script>
1

There are 1 answers

0
Kevin Babcock On

There were a few things wrong with the code you provided, the most notably being that you didn't set the role for your <ul> element. You need to change it to have the attribute data-role="listview". You also can't use an <li> element as the root element for a listview template (KendoUI automatically takes care of this for you), otherwise you'll get an error when the list is bound.

Here's an example on JS Bin.

And here's the code:

<!--Load Some Data-->
<div id="peopleDefaultView"
    data-role="view"
    data-model="viewModel"
    data-layout="flat">
    <!--View-->
    <input type="text" data-bind="value: firstName" id="fName" />
    <input type="text" data-bind="value: lastName" id="lName" />
    <a data-bind="click: addName" data-role="button" id="btnOne">Add</a>
    <div style="margin-top: 10px">
    People List:
    <ul id="pList"
        data-role="listview"
        data-template="people-l-template"
        data-bind="source: peopleList">
    </ul>
    </div>
</div>

<!-- Kendo Template-->
<script id="people-l-template" type="text/x-kendo-template">
    FirstName: <span>#:first#</span>
    LastName: <span>#:last#</span>
    <a id="btnRemoveName"
       data-role="button" 
       data-bind="click: removeName"
       data-first="#:first#" data-last="#:last#">
       X
    </a>
</script>

...

var viewModel = {
    firstName: null,
    lastName: null,
    peopleList: [],
    addName:  function (e) {
        var me = this;
        me.get('peopleList').push({
            first: me.get('firstName'),
            last: me.get('lastName')
        });
        me.set('firstName', '');
        me.set('lastName', '');
    },
    removeName: function (e) {
        var me = this;
        me.set('peopleList', $.grep(me.peopleList, function (item, i) {
            return item.first != e.target.data('first')
                && item.last != e.target.data('last');
        }));
    }
};

var application = new kendo.mobile.Application(document.body);