wycleffsean
wycleffsean

Reputation: 1377

Knockout JS: observableArray always remains empty

My goal is to produce a list of sortable elements that will be managed with knockout. For some reason unkown to me, however, the observableArray that I create will always remain empty. I feel as though I am missing something terribly obvious here...

<script type="text/javascript">
//var initialData = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));
//console.log(initialData);

var test = [{ Position: 1 },
            { Position: 2 },
            { Position: 3 },
            { Position: 4 },
            { Position: 5 }];

var viewModel = {};
    viewModel.Offers = ko.observableArray(test);

    viewModel.addOffer = function() {
        this.Offers.push({ Position : 5});
        console.log(this);
    };

$(function () {
    ko.applyBindings(viewModel, document.body);

    $( "#sortable" ).sortable().disableSelection();
});

JSFiddle of the problem

Upvotes: 0

Views: 596

Answers (1)

BluesRockAddict
BluesRockAddict

Reputation: 15683

You needed to wrap template name in single quotation marks:

<ul id="sortable" 
    data-bind="template: {name: 'offerTemplate', foreach: Offers}">

Here is working JSFiddle.

Upvotes: 3

Related Questions