2012-04-26 95 views
0

我的目標是生成一個可排序元素列表,這些元素將用knockout進行管理。但由於某種原因,我創建的observableArray將始終爲空。我感覺好像我在這裏的東西非常明顯......Knockout JS:observableArray始終保持爲空

<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

回答

3

您需要在單引號包裹模板名稱:

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

這裏工作JSFiddle