2017-01-25 19 views
0

を適用することはできません、私はデフォルトのバインディングforeachを上書きしたい:オーバーライドノックアウトは、バインディング:<strong>ノックアウト</strong>でバインディングを複数回エラー

var foreachInit = ko.bindingHandlers.foreach.init; 
ko.bindingHandlers.foreach.init = function() { 
    foreachInit.apply(this, arguments); 
}; 

をしかし、たとえ、この単純なスニペットはエラーをトリガ:

You cannot apply bindings multiple times to the same element

表示されるコンソールフィドルで:https://jsfiddle.net/hejdav/wxf51s5L/10/

あなたは知っています、なぜこれが起こっているのですか?

答えて

1

あなたはここで物事を考えすぎていると思います。 viewModelコンストラクタを作成し、必要なデータを初期化して渡し、それをobservableArrayに格納します。

var ViewModel = function(r) { 
 
    // if r exists place it in the observable otherwise it's an empty array 
 
    this.items = ko.observableArray(r || []); 
 
}; 
 

 
var model = new ViewModel([1, 2]); 
 
ko.applyBindings(model);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> 
 
<p data-bind="foreach: items"> 
 
    <span data-bind="text: $data"></span> 
 
</p>