jaffa
jaffa

Reputation: 27350

KnockoutJS mapping from JS not create an observable array

I thought the mapping plugin should return an observable array but the obbservable array is empty even though 'resp' response from server has 1000s of elements.

Am I missing something here? Please see below.

 <script type="text/javascript">         

        function tasksViewModel() {
            var self = this;

            self.tasks = ko.observableArray(null);

            self.load = function () {
                $.ajax({
                    url: '/api/benchmark',              
                    success: function(resp) {
                       // This line isn't working!
                       self.tasks = ko.mapping.fromJS(resp);
                    },
                    dataType: 'json'
                });
            }
        }

        var viewModel = new tasksViewModel();

        $(function () {

            ko.applyBindings(viewModel);
            viewModel.load();
        });

    </script>

Upvotes: 1

Views: 1695

Answers (1)

Ilya
Ilya

Reputation: 29673

self.tasks(resp);

you should set new value to existed observable array

Upvotes: 1

Related Questions