2012-07-07 40 views
0

我有一個問題,我的動態html正在被重新填充,當knockout.js從ajax調用中設置一個可觀察數組時。當發生這種情況時,動態HTML被重新加載,導致其中的init函數執行兩次並重新綁定其他所有內容。爲什麼我的html可觀察重新綁定動態HTML?我會使用小提琴,但我有很多代碼。動態HTML和knockout.js從Ajax重新綁定HTML請求

Ajax回調

app.viewModel.members.bracket.parts(response.Parts); 

ko.applyBindings(app.viewModel.members, app.viewModel.members.container().find('> div:first').get(0)); 

集裝箱

<div id="container" class="clearfix" data-bind="html: members.html, container: {}"> 
</div> 

<script type="text/javascript"> 
    app.viewModel.members = { 
     container: ko.observable($('#container')), 
     html: ko.observable(''), 
     bracket: { 
       parts: ko.observableArray([]) 
     } 
    }; 
</script> 

動態HTML

<div data-bind="with: members.bracket"> 
    <div> 
     <div id="bracket-wrapper"> 
      <div data-bind="template: { name: partTemplate, foreach: parts }"></div> 
     </div> 
    </div> 
</div> 

<script type="text/html" id="part-template-1"> 
    <div class="bracket-part" data-bind="bracketGameInit: { left: $data.left, top: $data.top, height: $data.height, width: $data.width }, css: { 'reverse-bracket' : $data.reverse }"></div> 
</script> 

app.members.bracket.init({ 
     pools: @Html.Raw(Model.DivisionPools.ToJSON()), 
     teams: @Html.Raw(Model.DivisionTeams.ToJSON()), 
     parts: @Html.Raw(Model.Parts.ToJSON()) 
    }); 

回答

0

人們所引起通過Observable Array Performance小費,我收到。我將其更新回舊的更新可觀察數組的方式。

function populateGameNumbers(gameNumbers) { 
     viewModel.gameNumbers([]); 

     var numbers = []; 
     for (var i = 0; i < gameNumbers; i++) { 
      numbers.push(i + 1); 
     } 
     viewModel.gameNumbers(numbers); 
     //viewModel.gameNumbers.push.apply(viewModel.gameNumbers, numbers); 
    }